所以,最近,我一直在试验多处理模块。我写了这个脚本来测试它:
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()
但是,当我尝试运行它时,会抛出此错误:
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
......我无法说出发生了什么。 有谁知道发生了什么,以及我如何解决它?
答案 0 :(得分:4)
将Process
所运行的函数的参数传递不同 - 查看它显示的documentation:
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()
或者在你的情况下:
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()
<强>增加:强>
Luke的好收获(在下面的评论中) - 您在执行以下操作时使用变量名a
覆盖函数a
:
a = Process(target=a, args=(3,))
您应该使用其他名称。