Skip to content

Commit cc78f52

Browse files
committed
Add a parameter to control the internal queue length
There is no good trade-off here …
1 parent c0afd44 commit cc78f52

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

trio_asyncio/async_.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _close(self):
8585

8686
@asynccontextmanager
8787
@async_generator
88-
async def open_loop():
88+
async def open_loop(queue_len=None):
8989
"""Main entry point: run an asyncio loop on top of Trio.
9090
9191
This is a context manager.
@@ -107,7 +107,7 @@ def _main_loop_exit(self):
107107

108108
async with trio.open_nursery() as nursery:
109109
old_loop = asyncio.get_event_loop()
110-
loop = TrioEventLoop()
110+
loop = TrioEventLoop(queue_len=queue_len)
111111
try:
112112
loop._closed = False
113113
asyncio.set_event_loop(loop)

trio_asyncio/base.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,13 @@ class BaseTrioEventLoop(asyncio.SelectorEventLoop):
107107
108108
* closed - nothing further may happen
109109
110+
Arguments:
111+
queue_len:
112+
The maximum length of the internal event queue.
113+
The default is 1000. Use more for large programs,
114+
or when running benchmarks.
110115
"""
116+
111117
# for calls from other threads or contexts
112118
_token = None
113119

@@ -127,9 +133,12 @@ class BaseTrioEventLoop(asyncio.SelectorEventLoop):
127133
# (threading) Thread this loop is running in
128134
_thread = None
129135

130-
def __init__(self, close_files=None):
136+
def __init__(self, queue_len=None):
137+
if queue_len is None:
138+
queue_len=1000
139+
131140
# Processing queue
132-
self._q = trio.Queue(9999)
141+
self._q = trio.Queue(queue_len)
133142

134143
# which files to close?
135144
self._close_files = set()

trio_asyncio/loop.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,12 @@ def run_trio_task(proc, *args):
212212
loop.run_trio_task(proc, *args)
213213

214214

215-
def run(proc, *args):
215+
def run(proc, *args, queue_len=None):
216216
"""Like :func:`trio.run`, but adds a context that supports asyncio.
217217
"""
218218

219219
async def _run_task(proc, args):
220-
async with open_loop():
220+
async with open_loop(queue_len=queue_len):
221221
return await proc(*args)
222222

223223
trio.run(_run_task, proc, args)

0 commit comments

Comments
 (0)