交互式Python中的多处理包

Dim*_*nev 6 python interactive multiprocessing

我有以下代码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 = Process(target=MyProcess, args=(a,))
    p.start()

    while(1):
       msg=b.recv()
       print(msg)
Run Code Online (Sandbox Code Playgroud)

如果我在DOS shell"python test.py"中执行它可以正常工作但是如果我使用IEP(Pyzo)的"执行文件"它不起作用.

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\pyzo2014a_64b\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:\pyzo2014a_64b\lib\multiprocessing\spawn.py", line 116, in _main
    self = pickle.load(from_parent)
AttributeError: Can't get attribute 'MyProcess' on <module '__main__' (built-in)>
Run Code Online (Sandbox Code Playgroud)

我发现这是一个记录在案的"问题".请检查以下链接的答案.

多处理在交互模式下中断

这是否意味着我不应该使用交互式Python中的多处理包?这是否意味着我无法从IPython控制台创建进程?对此的任何澄清将受到高度赞赏

Mik*_*rns 5

正确,您不能multiprocessing在解释器中使用……主要是因为pickle不知道如何序列化交互功能。但是,如果您使用multiprocessing名为的叉子,则pathos.multiprocessing可以从解释器执行所需的操作。这是有效的,因为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]
Run Code Online (Sandbox Code Playgroud)

到达pathos这里:https : //github.com/uqfoundation