无法从 spyder 运行 pathos 程序 IDE

Unable to run pathos program from spyder IDE

我有以下简单程序:

from pathos.core import connect

tunnel = connect('192.168.1.5', port=50004)

print(tunnel)
print(type(tunnel._lport))
print(tunnel._rport)

def sleepy_squared(x):
    from time import sleep
    sleep(1.0)
    return x**2

from pathos.pp import ParallelPythonPool as Pool
p = Pool(8, servers=('192.168.1.5:6260',))
print(p.servers)

x = [1, 2, 3, 4, 5, 6, 7, 8, 9]

y = p.map(sleepy_squared, x)
print(y)

当我从 Spyder 4 IDE 尝试 运行ning 这个程序时,我收到以下错误:

Tunnel('-q -N -L 4761:192.168.1.5:50004 192.168.1.5')
<class 'int'>
50004
('192.168.1.5:6260',)
Traceback (most recent call last):

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 3319, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-1-e89974d31563>", line 20, in <module>
y = p.map(sleepy_squared, x)

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/pathos/parallel.py", line 234, in map
return list(self.imap(f, *args))
  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/pathos/parallel.py", line 247, in imap
return (subproc() for subproc in list(builtins.map(submit, *args)))

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/pathos/parallel.py", line 243, in submit
return _pool.submit(f, argz, globals=globals())

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/pp/_pp.py", line 499, in submit
sfunc = self.__dumpsfunc((func, ) + depfuncs, modules)
  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/pp/_pp.py", line 683, in __dumpsfunc
sources = [self.__get_source(func) for func in funcs]

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/pp/_pp.py", line 683, in <listcomp>
sources = [self.__get_source(func) for func in funcs]

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/pp/_pp.py", line 750, in __get_source
self.__sourcesHM[hashf] = importable(func)

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/dill/source.py", line 957, in importable
src = _closuredimport(obj, alias=alias, builtin=builtin)
  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/dill/source.py", line 876, in _closuredimport
src = getimport(func, alias=alias, builtin=builtin)

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/dill/source.py", line 764, in getimport
return _getimport(head, tail, alias, verify, builtin)

  File "/home/mahmoud/anaconda3/envs/trade_fxcm/lib/python3.6/site-packages/dill/source.py", line 713, in _getimport
try: exec(_str) #XXX: check if == obj? (name collision)

  File "<string>", line 1
from __main__'> import sleepy_squared
                                     ^
SyntaxError: EOL while scanning string literal

当我从终端使用以下命令 运行 这个程序时 python test_connect.py 该程序工作正常。我的问题是为什么程序 运行ning 不在 Spyder IDE 4 上,我怎样才能在 Spyder IDE 4 上制作程序 运行?

我是 pathos 作者。 Spyder、Jupyter 和其他 IDE 在解释器之上添加了一个额外的执行层,在某些情况下,甚至将执行包装在闭包中以向 IDE 的其余部分添加额外的挂钩。您正在使用 ParallelPool,它使用 ppft,它使用 dill.source 通过提取对象的源代码及其依赖项来“序列化”。由于 IDE 正在添加一个闭包层,dill.source 也必须尝试对其进行序列化,但没有成功——所以简而言之,这是 dill.source 和 Spyder 之间的兼容性问题。如果您选择其他 pathos 个池之一,它可能会成功。 ProcessPool 本质上与 ParallelPool 相同,但按对象而不是按源代码序列化——它使用 multiprocess,后者使用 dill。然后是 ThreadPool,这可能是最有可能成功的,除非 Spyder 也扰乱了主线程——大多数 IDE 都会这样。所以你能对它做点啥?简单的事情是不要 运行 从 IDE 并行代码。本质上,在 IDE 中编写您的代码,然后换出 Pool,它应该并行 运行。 IDEs 通常不适合并行计算。