Python Multiprocessing : Processes Do Not Start
Solution 1:
You write:
Process(target=func(10)).start()
This means that the Process
never is given func
, it is given the result of func(10)
since Python first evaluates the arguments left-to-right, and then passes the result of these evaluations to the outer function (here the Process(..)
call).
In order to let the subprocess evaluate the function, you should write it like:
Process(target=func,args=(10,)).start()
Now you pass a reference to the func
function, and you provide a tuple of arguments args
with which the sub process is going to call the function. The subprocess will then call func(10)
itself (and another subprocess will almost concurrently do the same with func(1)
).
Solution 2:
When creating a Process
, target
should be function name only without arguments or parantheses and then you add args=(your args)
. For example:
Process(target=func, args=(10,)).start()
Solution 3:
Using Willem Van Onsem answer:
from time import sleep
from multiprocessing import Process
import multiprocessing
def func(x, called_from):
print("start %s from %s"%(x, called_from))
sleep(x)
print("end %s from %s"%(x, called_from))
return
if __name__ == '__main__':
Process(target=func, args=(10,'1')).start()
Process(target=func, args=(1,'2')).start()
You can use a lot of Process more to see when they're called and when they finish. Use other Process calls to see how they work.
Post a Comment for "Python Multiprocessing : Processes Do Not Start"