@@ -121,7 +121,7 @@ or (Python 3.7 ++)::
121121to this::
122122
123123 async def trio_main():
124- await trio_asyncio.run_asyncio (async_main)
124+ await trio_asyncio.aio_as_trio (async_main)( )
125125
126126 def main():
127127 trio_asyncio.run(trio_main)
@@ -152,12 +152,12 @@ Interrupting the asyncio loop
152152
153153Does not work: ``run_until_complete `` / ``run_forever `` are no longer
154154supported. You need to refactor your main loop. Broadly speaking, this
155- means to replace::
155+ means that you need to replace this code ::
156156
157157 async def setup():
158- pass # … do whatever
158+ pass # … start your services
159159 async def shutdown():
160- pass # … do whatever
160+ pass # … terminate services and clean up
161161 loop.stop()
162162
163163 loop = asyncio.get_event_loop()
@@ -166,37 +166,39 @@ means to replace::
166166
167167with::
168168
169+ stopped_event = trio.Event()
169170 async def setup():
170- pass
171- async def shutdown():
172- pass # … do whatever
171+ pass # … start your services
172+ async def cleanup():
173+ pass # … terminate services and clean up
174+ async def shutdown(): # no longer needs to be async
173175 stopped_event.set()
174176
175177 async def async_main():
176- await setup()
178+ await aio_as_trio( setup) ()
177179 await stopped_event.wait()
178- trio_asyncio.run(trio_asyncio.run_asyncio, async_main)
179-
180- ``trio_asyncio `` still contains code for supporting ``run_until_complete ``
181- and ``run_forever `` because it is required to run ``asyncio ``'s testcases,
182- most of which require these calls.
183-
184- However, to run this in production you'd need to jump through
185- a couple of hoops which are neither supported nor documented. Sorry.
180+ await aio_as_trio(cleanup)()
181+ trio_asyncio.run(async_main)
186182
187183Detecting the loop context
188184--------------------------
189185
190- Short answer: You don't want to.
186+ :func: `sniffio.current_async_library ` correctly reports "asyncio" when
187+ running in an asyncio context. (This requires Python 3.7, for
188+ :mod: `contextvars ` support in :mod: `asyncio `).
191189
192- Long answer: You either are running within a call to :func: `trio_asyncio.run_asyncio `,
193- or you don't. There is no "maybe" here, and you shouldn't indiscriminately
190+ However, this feature should not be necessary because
191+ your code either is running within a call to :func: `trio_asyncio.aio_as_trio `,
192+ or it is not. There should not be any "maybe" here: you shouldn't indiscriminately
194193call async code from both contexts – they have different semantics, esp.
195194concerning cancellation.
196195
197- If you really need to do this, :func: `sniffio.current_async_library `
198- correctly reports "asyncio" when appropriate. (This requires Python 3.7
199- for :mod: `contextvars ` support in :mod: `asyncio `.
196+ The only reasonable use for detecting the loop context is as ::
197+
198+ assert sniffio.current_async_library() == "trio"
199+
200+ (or "asyncio"), to detect mismatched contexts while porting code
201+ from :mod: `asyncio ` to :mod: `trio `.
200202
201203.. _cross-calling :
202204
@@ -206,18 +208,18 @@ for :mod:`contextvars` support in :mod:`asyncio`.
206208
207209First, a bit of background.
208210
209- For historical reasons, running an async function (of any
210- flavor) with Python is a two-step process – that is, given ::
211+ For historical reasons, calling an async function (of any
212+ flavor) is a two-step process – that is, given ::
211213
212214 async def proc():
213- whatever()
215+ pass
214216
215217a call to ``await proc() `` does two things:
216218
217219 * ``proc() `` creates an ``awaitable ``, i.e. something that has an
218220 ``__await__ `` method.
219221
220- * ``await proc() `` thus iterates this awaitable until it ends,
222+ * ``await proc() `` then iterates this awaitable until it ends,
221223 supported by your event loop's runtime system.
222224
223225:mod: `asyncio ` traditionally uses awaitables for indirect procedure calls,
@@ -241,7 +243,7 @@ Trio, in contrast, uses (async) callables::
241243 await proc()
242244 await run(some_code)
243245
244- Here, calling ``proc `` multiple times is not a problem.
246+ Here, calling ``proc `` multiple times from within `` run `` is not a problem.
245247
246248:mod: `trio_asyncio ` adheres to Trio conventions, but the
247249:mod: `asyncio ` way is also supported when possible.
0 commit comments