Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing: Process object not callable

So, recently, 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 I can fix it?

like image 919
Johnny Sasaki Avatar asked Feb 12 '26 14:02

Johnny Sasaki


1 Answers

Pass arguments to the function that is ran by a 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's 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()

Addition:
Good catch by Luke (in the comments below) - you're overriding the function a with the variable name a when doing:

a = Process(target=a, args=(3,))

You should use a different name.

like image 178
Nir Alfasi Avatar answered Feb 14 '26 05:02

Nir Alfasi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!