调试时在 pool.close() 上抛出异常,但在 运行 上抛出异常

Exception thrown on pool.close() while debugging, but not while running

我不认为我在 Python 2.7 中遇到这个问题,但是在 3.7 中调试时,Python 在调用 pool.close() 时抛出异常。这是函数的相关部分:

pool = multiprocessing.Pool(6)
iterator = pool.imap_unordered(worker_func, worker_input)

while True:
    try:
        t0, t1 = next(iterator)
    except multiprocessing.TimeoutError:
        continue
    except StopIteration:
        break
    else:
        dbinserts1(t0)
        dbinserts2(t1)            

pool.close()
pool.join()

2to3 所做的唯一更改是将 iterator.next() 重写为 next(iterator)。该函数仅在调试时失败(在 PyCharm 中),否则运行成功。这是(可能)堆栈跟踪中最相关的部分:

Error in atexit._run_exitfuncs: Traceback (most recent call last):
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/util.py", line 322, in _exit_function p.join() File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/process.py", line 138, in join assert self._parent_pid == os.getpid(), 'can only join a child process'
AssertionError: can only join a child process

您使用哪个 PyCharm 版本?这似乎已在 2019.1.2 中由 https://youtrack.jetbrains.com/issue/PY-34436

修复