File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
230237async def run_asyncio (proc , * args ):
231238 """Run an asyncio function or method from Trio.
You can’t perform that action at this time.
0 commit comments