使用pdb运行时,多处理代码失败?

时间:2019-07-19 19:57:55

标签: python python-3.x python-multiprocessing pdb

Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__'有关,但是由于不同情况引起的。

当我尝试使用pdb运行多处理代码时,我在Python 3.7.4中遇到问题。该问题与https://docs.python.org/3.6/library/multiprocessing.html中的基本多处理示例重复出现:

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

当通过[1, 4, 9]从Python直接运行时,运行良好(输出python.exe testcase.py)。但是,它在pdb下不起作用。 python.exe -m pdb testcase.py失败并显示错误:

Traceback (most recent call last):
  File "c:\python37\lib\pdb.py", line 1697, in main
    pdb._runscript(mainpyfile)
  File "c:\python37\lib\pdb.py", line 1566, in _runscript
    self.run(statement)
  File "c:\python37\lib\bdb.py", line 585, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "c:\users\max\desktop\projects\errortest.py", line 1, in <module>
    from multiprocessing import Pool
  File "c:\python37\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "c:\python37\lib\multiprocessing\pool.py", line 176, in __init__
    self._repopulate_pool()
  File "c:\python37\lib\multiprocessing\pool.py", line 241, in _repopulate_pool
    w.start()
  File "c:\python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "c:\python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "c:\python37\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "c:\python37\lib\multiprocessing\spawn.py", line 172, in get_preparation_data
    main_mod_name = getattr(main_module.__spec__, "name", None)
AttributeError: module '__main__' has no attribute '__spec__'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\python37\lib\multiprocessing\spawn.py(172)get_preparation_data()
-> main_mod_name = getattr(main_module.__spec__, "name", None)

我毫不犹豫地认为,我已经在一对模块中发现了一个错误,这些错误已成为Python十多年来的重要组成部分。这里有什么不对吗?

1 个答案:

答案 0 :(得分:2)

这是Windows中多重处理的限制。 This问题很好地解释了为什么会这样。谷歌的快速搜索显示puDB可能可以帮助调试多处理代码,但我以前从未使用过。

以下来自python docs

此软件包中的功能要求 main 模块可由子级导入。编程指南中对此进行了介绍,但是这里值得指出。 这意味着某些示例,例如X=X.values y=y.values from sklearn.preprocessing import StandardScaler scaler=StandardScaler() X_train=scaler.fit_transform(X_train) X_test=scaler.fit_transform(X_test) 示例在交互式解释器中将不起作用。