import random import time from concurrent.futures import ProcessPoolExecutor from threading import Thread import asyncio WORKERS = 16 ASYNC_WORKERS = 8 def do_async(i): async def spin(): print(f"{i}: async_spin()") await asyncio.sleep(3) asyncio.run(spin()) def spin(i): print(f"{i}: subproc_spin()") time.sleep(3) def do_in_subproc(i): with ProcessPoolExecutor(max_workers=1) as executor: executor.submit(spin, i).result() threads = [ Thread( target=do_async if i < ASYNC_WORKERS else do_in_subproc, args=[i] ) for i in range(0, WORKERS) ] random.shuffle(threads) for thread in threads: thread.start() for thread in threads: thread.join()