Python multiprocessing: process object not callable
So, lately I've been experimenting with the multiprocessing module. I wrote this script to test it:
from multiprocessing import Process
from time import sleep
def a(x):
sleep(x)
print ("goo")
a = Process(target=a(3))
b = Process(target=a(5))
c = Process(target=a(8))
d = Process(target=a(10))
if __name__ == "__main__":
a.start()
b.start()
c.start()
d.start()
However, when I try to run it, it throws this error:
goo
Traceback (most recent call last):
File "C:\Users\Andrew Wong\Desktop\Python\test.py", line 9, in <module>
b = Process(target=a(5))
TypeError: 'Process' object is not callable
... And I can't tell what's going on. Does anyone know what happened and how can I fix it?
+3
source to share
1 answer
Passing arguments to a function executed with Process
is done differently - looking at the documentation it shows:
from multiprocessing import Process
def f(name):
print 'hello', name
if __name__ == '__main__':
p = Process(target=f, args=('bob',)) # that how you should pass arguments
p.start()
p.join()
Or in your case:
from multiprocessing import Process
from time import sleep
def a(x):
sleep(x)
print ("goo")
e = Process(target=a, args=(3,))
b = Process(target=a, args=(5,))
c = Process(target=a, args=(8,))
d = Process(target=a, args=(10,))
if __name__ == "__main__":
e.start()
b.start()
c.start()
d.start()
Addendum:
Nice catch from Luke (in the comments below) - you override the function a
with a variable name a
when you execute:
a = Process(target=a, args=(3,))
You must use a different name.
+4
source to share