简体   繁体   English

如何在python中停止多进程

[英]how to stop a multiprocess in python

When trying to run multiprocessing on a for loop all the process are being executed but the program is not terminated and keep running.当尝试在 for 循环上运行多处理时,所有进程都在执行,但程序没有终止并继续运行。

How can i stop the program after all elements in the list got processed?处理完列表中的所有元素后,如何停止程序?

def multiprocess_for_loop(*args, list_to_iter_over, function):
# the element in iterable is the first argument in the function
output = Queue()
processes = [Process(target=function, args=(elem,) + args) for elem in list_to_iter_over]
logging.info(f"start multiprocess for-loop on list: {list_to_iter_over}")
for p in processes:
    p.start()

results = []
for p in processes:
    results.append(output.get())

for p in processes:
    p.join()

logging.info("finished multiprocessing for-loop")
return results

Every process write on Queue?每个进程都写在队列上? If a process don't write in it, your code keep stuck in that for.如果一个进程没有写进去,你的代码就会一直卡在那里。

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

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