0

I have spawned multiple python processes and threads like:

from multiprocessing import Process
from threading import Thread

def thread():
    pass
    #some job

def process(threads_nr):
    for i in range(threads_nr):
        d = Thread(target=thread,args=())
        d.setDaemon(True)
        d.start()

data_list = ['group1','group2']

for data in data_list:
    proc = Process(target=process, args=(2,))    
    proc.daemon = True 
    proc.start()
#doing something else 

and noticed that all threads are running on same cpu (the cpu nr is on the left): enter image description here

does anybody have any idea why ?

Toma Dragos
  • 11
  • 1
  • 5
  • 2
    It is probably an artifact of the operating system's scheduler in this particular scenario. However, it looks to me like it is moot ... since the processes look like they are hardly consuming any CPU. My guess is that they are either I/O bound, or you have far too many active processes for the number of physical cores available to your application – Stephen C Apr 30 '20 at 10:51
  • system decides which CPU to use to run processes. If other CPUs are too buzy then all your processes may run to the same CPU. BTW: usign Google "python decide which CPU to use" I found answer https://stackoverflow.com/a/36796927/1832058 and it use some native function in Windows to decide which CPU to use. Linux may have also something similar but I don't know it. https://unix.stackexchange.com/a/23109/151882 – furas Apr 30 '20 at 11:13
  • I found [os.sched_setaffinity()](https://www.geeksforgeeks.org/python-os-sched_setaffinity-method/) to control which CPU can be assigned to process (in some Unix) – furas Apr 30 '20 at 11:23

0 Answers0