Created
December 29, 2020 10:02
-
-
Save gsalgado/223267e42657d62b03ad50af2ae104f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from concurrent.futures import ProcessPoolExecutor | |
import time | |
import trio | |
from trio_run_in_process import open_worker_pool | |
task_count = 1000 | |
task_length = 0.1 | |
n_workers = 100 | |
def noop(secs): | |
time.sleep(secs) | |
async def async_noop(secs): | |
noop(secs) | |
async def main_asyncio(): | |
start = time.monotonic() | |
executor = ProcessPoolExecutor(max_workers=n_workers) | |
loop = asyncio.get_event_loop() | |
tasks = [] | |
for _ in range(task_count): | |
tasks.append(loop.run_in_executor(executor, noop, 0.1)) | |
for task in tasks: | |
await task | |
end = time.monotonic() | |
print(f"run_in_executor(): {end - start}") | |
async def trio_run_in_pool(pool, send_chan): | |
await pool.run(async_noop, 0.1) | |
await send_chan.send(None) | |
async def main_trio(): | |
async with trio.open_nursery() as nursery: | |
send_chan, recv_chan = trio.open_memory_channel(n_workers) | |
async with open_worker_pool(n_workers) as pool: | |
start = time.monotonic() | |
for _ in range(task_count): | |
nursery.start_soon(trio_run_in_pool, pool, send_chan) | |
for _ in range(task_count): | |
await recv_chan.receive() | |
end = time.monotonic() | |
print(f"worker_pool.run(): {end - start}") | |
print(f"{task_count} tasks, spending {task_length} seconds, using {n_workers} workers") | |
asyncio.run(main_asyncio()) | |
trio.run(main_trio) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment