Python 多处理:进程对象不可调用
Python multiprocessing: Process object not callable
所以,最近,我一直在试验多处理模块。我写了这个脚本来测试它:
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
...我不知道发生了什么。
有谁知道发生了什么,我该如何解决?
通过 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,))
您应该使用不同的名称。
所以,最近,我一直在试验多处理模块。我写了这个脚本来测试它:
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
...我不知道发生了什么。 有谁知道发生了什么,我该如何解决?
通过 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,))
您应该使用不同的名称。