You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unix环境Python3.7中queue.close()触发后台BrokenPipeError是否为Bug?

关于Python 3.7 multiprocessing.Queue的BrokenPipeError问题

在Unix系统上运行以下Python 3.7代码:

import multiprocessing

queue = multiprocessing.Queue()
queue.put(1)
# time.sleep(0.01)  添加此行可避免报错
queue.close()

会在后台触发BrokenPipeError,异常追踪信息如下:

Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/usr/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/usr/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/usr/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe

我认为这明显是个Bug,毕竟queue.close()和queue.join_thread()的存在就是为了避免这类问题。是不是我忽略了什么?

需要说明的是,这个BrokenPipeError只会在Python内部给队列提供数据的后台线程中触发,主进程不会感知到该错误,唯一影响就是会打印出无意义的异常追踪信息。

内容的提问来源于stack exchange,提问作者agemO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 08:10:27