首页 > 解决方案 > 在 Python 中,我可以在一开始就启动一个后台工作程序,进行一些计算,然后在同一个函数结束时等待它的结果吗?

问题描述

在 Python 中,如何在函数的开头启动一个进程/工作者并在最后等待它?就像是:

def my_function():
    worker_ref = start_long_computation()
    local_result = do_heavy_computations()
    return worker_ref.wait_for_result() or local_result

这样,wait_for_result()如果结果已在start_long_computation()和之间处理,则可能会立即返回do_heavy_computations()

标签: pythonbackgroundworker

解决方案


推荐阅读