简体   繁体   English

在Python中丢失排队的任务apply_async

[英]Losing queued tasks in Python apply_async

I am attempting to write a wrapper that iterates through running another program with different input files. 我正在尝试编写一个包装,该包装通过使用不同的输入文件运行另一个程序来进行迭代。 The program (over which I have no control, but need to use) needs to be run out of the same directory as the input file(s). 该程序(我无法控制,但需要使用它)需要从与输入文件相同的目录中运行。 So far my method is to use OS-module to change/create directory structure, use apply-async to run the program given a sub-directory, and each child in apply-async changes directory, creates the file, and the first 8 processes run successfully (i have 8 virtual cores) 到目前为止,我的方法是使用OS模块更改/创建目录结构,使用apply-async运行给定子目录的程序,apply-async中的每个子更改目录,创建文件,以及前8个进程运行成功(我有8个虚拟核心)

However, I am queueing up to 100 of these processes (they run a simulation which takes a few minutes, I'm looking to optimize). 但是,我最多要对100个这样的进程进行排队(它们运行的​​仿真需要几分钟,我希望进行优化)。 I use "call" on the outside executable I am running. 我在运行的外部可执行文件上使用“调用”。 I thought everything was going great, but then after the 8th simulation runs, everything stops, I check 0 processes are running. 我以为一切都很好,但是在第8次模拟运行之后,一切都停止了,我检查了0个进程是否在运行。 It is as if the queue forgot about the other processes. 好像队列忘记了其他进程。

What can I do to fix this? 我该怎么做才能解决此问题? I know my RAM only goes up about 300 MB out of 8GB. 我知道我的RAM在8GB中仅增加了约300 MB。

Do I need to look into implementing some sort of queue myself that waits for the exit code of the simulation executable? 我是否需要自己研究某种等待仿真可执行文件退出代码的队列?

Thank you in advance. 先感谢您。

Maybe better than nothing. 也许总比没有好。 This shows a correct way to use apply_async() , and demonstrates that - yup - there's no problem creating many more tasks than processes. 这显示了使用apply_async()的正确方法,并演示了-是的-创建比进程更多的任务没有问题。 I'd tell you how this differs from what you're doing, but I have no idea what you're doing ;-) 我会告诉你这和你在做什么有什么不同,但是我不知道你在做什么;-)

import multiprocessing as mp

def work(i):
    from time import sleep
    sleep(2.0 if i & 1 else 1.0)
    return i*i

if __name__ == "__main__":
    pool = mp.Pool(4)
    results = [pool.apply_async(work, (i,)) for i in range(100)]
    results = [r.get() for r in results]
    print len(results), results
    pool.close()
    pool.join()

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

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