Skip to content

Commit e142699

Browse files
committed
Iterator support
1 parent 8c147ff commit e142699

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

trio_asyncio/base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,23 @@ async def run_coroutine(self, coro):
215215
def wrap_generator(self, gen, *args):
216216
return run_generator(self, gen(*args))
217217

218+
def run_iterator(self, aiter):
219+
"""Call an asyncio iterator from Trio.
220+
221+
Example Usage::
222+
async def slow_nums():
223+
n = 0
224+
while True:
225+
asyncio.sleep(1)
226+
yield n
227+
n += 1
228+
229+
async def trio_code(loop):
230+
async for n in loop.run_iterator(slow_nums()):
231+
print(n)
232+
"""
233+
return run_generator(self, aiter)
234+
218235
async def run_asyncio(self, proc, *args):
219236
"""Run an asyncio function or method from Trio.
220237

trio_asyncio/loop.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'run_coroutine',
2828
'run_asyncio',
2929
'wrap_generator',
30+
'run_iterator',
3031
'TrioChildWatcher',
3132
'TrioPolicy',
3233
]
@@ -226,6 +227,12 @@ def wrap_generator(proc, *args):
226227
raise RuntimeError("Need to run in a trio_asyncio.open_loop() context")
227228
return loop.wrap_generator(proc, *args)
228229

230+
def run_iterator(aiter):
231+
loop = asyncio.get_event_loop()
232+
if not isinstance(loop, TrioEventLoop):
233+
raise RuntimeError("Need to run in a trio_asyncio.open_loop() context")
234+
return loop.run_iterator(aiter)
235+
229236

230237
async def run_asyncio(proc, *args):
231238
"""Run an asyncio function or method from Trio.

0 commit comments

Comments
 (0)