How to kill all processes when one of them has finished using concurrent.futures?

Viewed 236

I need to kill all processes when any of them has finished. Here is my code:

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)

I tried doing it with wait but it doesn't work.

2 Answers

I dont have any way of testing currently, but I think this is sufficient:

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)

Should be no need to run through the results an additional time.

Thanks for the help! I got this design. If you have any ideas on how to simplify the code, please submit them too.

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)
Related