Created
January 11, 2021 09:15
-
-
Save gsalgado/8631145aa42a95ba44a645796e444b46 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 secrets | |
import time | |
import trio | |
from trio_run_in_process import open_worker_pool | |
task_count = 1000 | |
n_workers = 1 | |
async def noop(d): | |
return d | |
class Foo: | |
def __init__(self, arg): | |
self.arg = arg | |
async def do_run(self, d): | |
return None | |
f = Foo(None) | |
async def trio_run_in_pool(pool, d, send_chan): | |
_ = await pool.run(f.do_run, d) | |
await send_chan.send(None) | |
async def main_trio(): | |
data = secrets.token_bytes(1024 ** 2) | |
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 i in range(task_count): | |
nursery.start_soon(trio_run_in_pool, pool, data, 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, using {n_workers} workers") | |
trio.run(main_trio) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment