2

我有 Python 3.8 并且无法升级(我有仅适用于 Python 3.8 的依赖项),所以这篇 SO 帖子没有回答我的问题。如何防止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, 2, 3, 4, 5]
(2, 3, 4, 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,而且速度更快,但我不知道为什么。

4

0 回答 0