繁体   English   中英

Python 3.4中的多处理功能有问题吗?

[英]Is multiprocessing in Python 3.4 broken?

我正在尝试在Python 3中使用多处理模块。但是,每次尝试建立一个Pool时,都会得到以下回溯。

Traceback (most recent call last):
  File "testmp.py", line 7, in <module>
    with Pool(5) as p:
  File "/usr/lib/python3.4/multiprocessing/context.py", line 118, in Pool
    context=self.get_context())
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 150, in __init__
    self._setup_queues()
  File "/usr/lib/python3.4/multiprocessing/pool.py", line 243, in _setup_queues
    self._inqueue = self._ctx.SimpleQueue()
  File "/usr/lib/python3.4/multiprocessing/context.py", line 110, in SimpleQueue
    from .queues import SimpleQueue
  File "/usr/lib/python3.4/multiprocessing/queues.py", line 20, in <module>
    from queue import Empty, Full
ImportError: cannot import name 'Empty'

可以轻松触发此操作的最简单代码是多处理模块文档中的第一个示例。

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]))

(来源: https : //docs.python.org/3.4/library/multiprocessing.html

我的问题:这是Python 3.4中的错误吗? 还是我做错了什么? 等效的代码在Python 2.7中有效。

您有一个名为queue本地模块; 它会干扰标准库模块

删除它或重命名它,代码将再次起作用:

stackoverflow-3.4 mj$ cat test.py 
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]))

stackoverflow-3.4 mj$ touch queue.py
stackoverflow-3.4 mj$ bin/python test.py 
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    with Pool(5) as p:
  File "/.../python3.4/multiprocessing/context.py", line 118, in Pool
    context=self.get_context())
  File "/.../python3.4/multiprocessing/pool.py", line 150, in __init__
    self._setup_queues()
  File "/.../python3.4/multiprocessing/pool.py", line 243, in _setup_queues
    self._inqueue = self._ctx.SimpleQueue()
  File "/.../lib/python3.4/multiprocessing/context.py", line 110, in SimpleQueue
    from .queues import SimpleQueue
  File "/.../lib/python3.4/multiprocessing/queues.py", line 20, in <module>
    from queue import Empty, Full
ImportError: cannot import name 'Empty'
stackoverflow-3.4 mj$ rm queue.py
stackoverflow-3.4 mj$ bin/python test.py 
[1, 4, 9]

如果python3 -c 'import queue; print(queue.__file__)'它,请运行python3 -c 'import queue; print(queue.__file__)' python3 -c 'import queue; print(queue.__file__)'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM