交互式Python中的多处理包
我有以下代码test.py:交互式Python中的多处理包,python,multiprocessing,interactive,Python,Multiprocessing,Interactive,我有以下代码test.py: #multiprocessing in the interactive Python import time from multiprocessing import Process, Pipe def MyProcess(a): while(1): time.sleep(1) a.send("tic") if __name__ == "__main__": a, b = Pipe() p =
#multiprocessing in the interactive Python
import time
from multiprocessing import Process, Pipe
def MyProcess(a):
while(1):
time.sleep(1)
a.send("tic")
if __name__ == "__main__":
a, b = Pipe()
p = Process(target=MyProcess, args=(a,))
p.start()
while(1):
msg=b.recv()
print(msg)
如果我在DOS shell“python test.py”中执行它,效果会很好
但如果我使用IEP(Pyzo)中的“执行文件”,它就不起作用了
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\pyzo2014a\u 64b\lib\multiprocessing\spawn.py”,第106行,在spawn\u main中
出口代码=_主(fd)
文件“C:\pyzo2014a_64b\lib\multiprocessing\spawn.py”,第116行,在_main中
self=pickle.load(从父级)
AttributeError:无法在上获取属性“MyProcess”
我发现这是一个记录在案的“问题”。请检查下面链接的答案
这是否意味着我不应该使用交互式Python中的多处理包?这是否意味着我无法从IPython控制台创建进程?
对此的任何澄清都将受到高度赞赏正确,您不能从解释器使用
多处理
,主要是因为pickle
不知道如何序列化交互函数。但是,如果使用名为pathos.multiprocessing
的多处理
fork,则可以从解释器中执行所需操作。这是因为pathos.multiprocessing
使用dill
,并且dill
知道如何序列化解释器中定义的函数(和其他对象)
>>> from pathos.multiprocessing import ProcessingPool as Pool
>>>
>>> p = Pool(4)
>>> def squared(x):
... return x**2
...
>>> def pow(x,y):
... return x**y
...
>>> a = range(10)
>>> b = range(10,0,-1)
>>>
>>> p.map(squared, a)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> res = p.amap(pow, a, b)
>>> print "asynchronous, and with multiple inputs!"
asynchronous, and with multiple inputs!
>>> res.get()
[0, 1, 256, 2187, 4096, 3125, 1296, 343, 64, 9]
获取
pathos
此处:新的pathos
即将发布。现有的稳定版本非常旧,早于pip
,但是使用setuptools
,安装起来非常简单。谢谢你,迈克,我来看看并报告。
>>> from pathos.multiprocessing import ProcessingPool as Pool
>>>
>>> p = Pool(4)
>>> def squared(x):
... return x**2
...
>>> def pow(x,y):
... return x**y
...
>>> a = range(10)
>>> b = range(10,0,-1)
>>>
>>> p.map(squared, a)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> res = p.amap(pow, a, b)
>>> print "asynchronous, and with multiple inputs!"
asynchronous, and with multiple inputs!
>>> res.get()
[0, 1, 256, 2187, 4096, 3125, 1296, 343, 64, 9]