如何解决Python 3.8 concurrent.futures“OSError:句柄已关闭”
我有 Python 3.8 并且无法升级(我的依赖项仅适用于 Python 3.8),所以 this SO post 没有回答我的问题。如何防止 OSError: handle is closed
错误?当将结果返回到主线程时会发生这种情况。这是一个非常简单的例子:
from concurrent.futures import ProcessPoolExecutor
def my_func(num):
return num + 1
def worker(data):
with ProcessPoolExecutor(5) as executor:
result = executor.map(my_func,data)
return result
if __name__ == "__main__":
arr = [1,2,3,4,5]
print(arr)
arr = worker(arr)
print(tuple(arr))
这给了
[1,5]
(2,5,6)
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "C:\Users\hendra11\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py",line 102,in _python_exit
thread_wakeup.wakeup()
File "C:\Users\hendra11\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py",line 90,in wakeup
self._writer.send_bytes(b"")
File "C:\Users\hendra11\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py",line 183,in send_bytes
self._check_closed()
File "C:\Users\hendra11\AppData\Local\Programs\Python\Python38\lib\multiprocessing\connection.py",line 136,in _check_closed
raise OSError("handle is closed")
OSError: handle is closed
有谁知道是什么导致了这个错误以及我如何手动修复它?
如果有关系,我使用的是运行 Visual Studio Code 的 Windows 10。
更新:当我使用 ThreadPoolExecutor
时,这个错误消失了,而且速度更快,但我不知道为什么。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。