Skip to main content
 首页 » 编程设计

python之Python 3.4 中的多处理被破坏了吗

2025年05月04日15shihaiming

我正在尝试在 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 的本地模块;它会干扰 standard library module .

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

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;打印(队列.__file__)'。