如何在其中一个进程使用完 concurrent.futures 时终止所有进程?
How to kill all processes when one of them has finished using concurrent.futures?
我需要在其中任何一个完成时终止所有进程。这是我的代码:
with concurrent.futures.ProcessPoolExecutor() as executor:
db_datas = [[skins, stickers, proxies_list] for skins in skins_arrays]
results = [executor.submit(wrapper, db_data) for db_data in db_datas]
concurrent.futures.wait(results, timeout=10, return_when=concurrent.futures.FIRST_COMPLETED)
for f in concurrent.futures.as_completed(results):
f_success = f.result()
print(f_success)
我试过用 wait
做,但没用。
感谢您的帮助!我得到了这个设计。如果您对如何简化代码有任何想法,也请提交。
with concurrent.futures.ProcessPoolExecutor() as executor:
db_datas = [[skins, stickers, proxies_list] for skins in skins_arrays]
results = [executor.submit(wrapper, db_data) for db_data in db_datas]
concurrent.futures.wait(results, timeout=10, return_when=concurrent.futures.FIRST_COMPLETED)
for f in concurrent.futures.as_completed(results):
f_success = f.result()
for _ in concurrent.futures.as_completed(results):
executor.shutdown(wait=False, cancel_futures=True)
print(f_success)
我目前没有任何测试方法,但我认为这已经足够了:
with concurrent.futures.ProcessPoolExecutor() as executor:
db_datas = [[skins, stickers, proxies_list] for skins in skins_arrays]
results = [executor.submit(wrapper, db_data) for db_data in db_datas]
concurrent.futures.wait(results, timeout=10, return_when=concurrent.futures.FIRST_COMPLETED)
for f in concurrent.futures.as_completed(results):
f_success = f.result()
if f_success:
executor.shutdown(wait=False, cancel_futures=True)
应该不需要运行再检查一次结果。
我需要在其中任何一个完成时终止所有进程。这是我的代码:
with concurrent.futures.ProcessPoolExecutor() as executor:
db_datas = [[skins, stickers, proxies_list] for skins in skins_arrays]
results = [executor.submit(wrapper, db_data) for db_data in db_datas]
concurrent.futures.wait(results, timeout=10, return_when=concurrent.futures.FIRST_COMPLETED)
for f in concurrent.futures.as_completed(results):
f_success = f.result()
print(f_success)
我试过用 wait
做,但没用。
感谢您的帮助!我得到了这个设计。如果您对如何简化代码有任何想法,也请提交。
with concurrent.futures.ProcessPoolExecutor() as executor:
db_datas = [[skins, stickers, proxies_list] for skins in skins_arrays]
results = [executor.submit(wrapper, db_data) for db_data in db_datas]
concurrent.futures.wait(results, timeout=10, return_when=concurrent.futures.FIRST_COMPLETED)
for f in concurrent.futures.as_completed(results):
f_success = f.result()
for _ in concurrent.futures.as_completed(results):
executor.shutdown(wait=False, cancel_futures=True)
print(f_success)
我目前没有任何测试方法,但我认为这已经足够了:
with concurrent.futures.ProcessPoolExecutor() as executor:
db_datas = [[skins, stickers, proxies_list] for skins in skins_arrays]
results = [executor.submit(wrapper, db_data) for db_data in db_datas]
concurrent.futures.wait(results, timeout=10, return_when=concurrent.futures.FIRST_COMPLETED)
for f in concurrent.futures.as_completed(results):
f_success = f.result()
if f_success:
executor.shutdown(wait=False, cancel_futures=True)
应该不需要运行再检查一次结果。