3

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't solve it. It makes the first process run but fails running the other ones, ending up with the same error: TypeError: 'Process' object is not callable. I'm also investigating what caused this issue, since this is how the documentation puts it.
Beat me to it; quick note on this, there's a naming conflict between def a(x) and a = Process.... Renaming the variable or function fixes this and I get it running locally. @DJanssens the trailing comma in the args tuple is important, it won't run without it.
Luke! Thanks! I fixed the naming error, and now it works!
Fixing the naming conflict solves the issue Luke Merret! nice one ;) I suggest someone posts this as a real answer or @alfasin updates his.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.