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

使用ProcessPoolExecutor递归调用时程序挂起问题求助

问题描述

尝试在递归调用中使用ProcessPoolExecutor但无法正常运行,以下是最简复现示例:

from concurrent.futures import ProcessPoolExecutor
from time import sleep

executor = ProcessPoolExecutor()
i = 3

def test():
    global i
    print("top")
    if i == 0:
        print("bar")
        return
    else:
        print("foo")
        i -= 1
        p = executor.submit(test)
        print(p.result())
        print(i)

test()

预期流程:i递减,在进程池发起递归调用并等待结果,直到i为0后逐层返回。但实际仅输出:

top
foo
top
foo

之后程序挂起。

可能的问题排查

竞态条件

猜测可能是竞态条件导致i未正确递减,但即便如此函数也应被多次调用。

全局状态

移除全局状态依赖,改用随机概率触发递归,程序仍在两次调用后挂起。

补充:使用Ubuntu 23系统


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 10:24:51