在Python 3.4中,多处理是否被破坏?

时间:2022-02-27 10:46:57

I'm trying to use the multiprocessing module in Python 3. However, everytime I try to set up a Pool, I get the following traceback.

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

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'

The simplest code that can easily trigger this is the first example in the multiprocessing module documentation.

易于触发的最简单代码是多处理模块文档中的第一个示例。

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

(source: https://docs.python.org/3.4/library/multiprocessing.html)

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

My question: Is this a bug in Python 3.4? Or am I doing something wrong? The equivalent code works in Python 2.7.

我的问题是:这是Python 3.4中的一个bug吗?还是我做错了什么?等效代码在Python 2.7中工作。

1 个解决方案

#1


6  

You have a local module named queue; it interferes with the standard library module.

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

Remove it or rename it and your code will work again:

删除它或重命名它,您的代码将再次工作:

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

*-3.4 mj$ touch queue.py
*-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'
*-3.4 mj$ rm queue.py
*-3.4 mj$ bin/python test.py 
[1, 4, 9]

If you have trouble locating it, run python3 -c 'import queue; print(queue.__file__)'.

如果您很难找到它,运行python3 -c的导入队列;打印(queue.__file__)”。

#1


6  

You have a local module named queue; it interferes with the standard library module.

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

Remove it or rename it and your code will work again:

删除它或重命名它,您的代码将再次工作:

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

*-3.4 mj$ touch queue.py
*-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'
*-3.4 mj$ rm queue.py
*-3.4 mj$ bin/python test.py 
[1, 4, 9]

If you have trouble locating it, run python3 -c 'import queue; print(queue.__file__)'.

如果您很难找到它,运行python3 -c的导入队列;打印(queue.__file__)”。