import asyncio
from time import time, sleep
from concurrent.futures import ProcessPoolExecutor
def some_blocking_task(x, y, z):
sleep(1)
return x + y + z
async def with_async(loop):
with ProcessPoolExecutor() as pool:
promises = [loop.run_in_executor(pool, some_blocking_task, 1, 2, 3) for _ in range(40)]
return await asyncio.gather(*promises)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
start = time()
loop.run_until_complete(with_async(loop))
print("async:", time() - start)
async def on_message(self, message):
futures = [client.write_message(message) client in ws_clients]
await asyncio.gather(*futures)