首页 > 解决方案 > Python 多处理运行自己而不是导入的脚本

问题描述

尝试使用 Python 进行多处理的新手,但是根据我在本网站和其他地方阅读的内容,我所经历的没有任何意义。这是我的代码:

import multiprocessing as mp
import some_func

cpu_count = mp.cpu_count()
print(cpu_count)

## Some code to build list of function arguments in list 'tasks'

pool = mp.Pool(cpu_count)
if __name__ == '__main__':
    for x in tasks:
        pool.apply_async(some_func.func, x)
    pool.close()
    pool.join()

some_func.func 的第一行是: print('running import',first_variable) 所以每次启动函数时我都应该清楚地看到。

相反,我看到 cpu_count 变量连续打印出来,而 some_func.func 从未被调用。

标签: pythonmultiprocessing

解决方案


推荐阅读