Search code examples
pythonconcurrent.futures

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


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.


Solution

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