Python 3.4 中的多处理是否损坏?
Is multiprocessing in Python 3.4 broken?
我正在尝试使用 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'
可以轻松触发的最简单代码是多处理模块文档中的第一个示例。
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
的 local 模块;它会干扰 standard library module.
删除它或重命名它,您的代码将再次运行:
Whosebug-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]))
Whosebug-3.4 mj$ touch queue.py
Whosebug-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'
Whosebug-3.4 mj$ rm queue.py
Whosebug-3.4 mj$ bin/python test.py
[1, 4, 9]
如果您找不到它,运行 python3 -c 'import queue; print(queue.__file__)'
。
我正在尝试使用 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'
可以轻松触发的最简单代码是多处理模块文档中的第一个示例。
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
的 local 模块;它会干扰 standard library module.
删除它或重命名它,您的代码将再次运行:
Whosebug-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]))
Whosebug-3.4 mj$ touch queue.py
Whosebug-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'
Whosebug-3.4 mj$ rm queue.py
Whosebug-3.4 mj$ bin/python test.py
[1, 4, 9]
如果您找不到它,运行 python3 -c 'import queue; print(queue.__file__)'
。