简体   繁体   English

Python 多处理在不退出文件的情况下失去活动

[英]Python multiprocessing loses activity without exiting file

I have a problem where my.py file, which uses maximum CPU through multiprocessing, stops operating without exiting the.py file.我有一个问题,my.py 文件通过多处理使用最大 CPU,在不退出 .py 文件的情况下停止运行。

I am running a heavy task that uses all cores in an old MacBook Pro (2012).我正在执行一项繁重的任务,该任务使用旧 MacBook Pro (2012) 中的所有内核。 The task runs fine at first, where I can visually see four python3.7 tasks populate the Activity Monitor window.该任务一开始运行良好,我可以直观地看到四个 python3.7 任务填充了活动监视器 window。 However, after about 20 minutes, those four python3.7 disappear from the Activity Monitor.然而,大约 20 分钟后,这四个 python3.7 从活动监视器中消失了。

The strangest part is the multiprocessing.py file is still operating, ie it never threw an uncaught exception nor exited the file.最奇怪的部分是 multiprocessing.py 文件仍在运行,即它从未抛出未捕获的异常或退出文件。

Would you guys/gals have any ideas as to what's going on?你们对发生的事情有什么想法吗? My guess is 1) it's most likely an error in the script, and 2) the old computer is overheating.我的猜测是 1)这很可能是脚本中的错误,以及 2)旧计算机过热。

Thanks!谢谢!

Edit: Below is the multiprocess code, where the multiprocess function to execute is func with a list as its argument.编辑:下面是多进程代码,其中要执行的多进程 function 是func ,其参数为列表。 I hope this helps!我希望这有帮助!

import multiprocessing

def main():
    pool = multiprocessing.Pool()
    for i in range(24):
        pool.apply_async(func, args = ([], ))
    pool.close()
    pool.join()

if __name__ == '__main__':
    main()

Use a context manager to handle closing processes properly.使用上下文管理器正确处理关闭进程。

from multiprocessing import Pool

def main():
    with Pool() as p:
        result = p.apply_async(func, args = ([], ))
        print(result)

if __name__ == '__main__':
    main()

I wasn't sure what you were doing with the for i in range() part.我不确定你对for i in range()部分做了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM