Skip to content

Commit 023713d

Browse files
committed
document deprecations and aio_as_trio
1 parent a0a29da commit 023713d

3 files changed

Lines changed: 53 additions & 27 deletions

File tree

docs/source/usage.rst

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ or (Python 3.7 ++)::
121121
to 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

153153
Does not work: ``run_until_complete`` / ``run_forever`` are no longer
154154
supported. 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
167167
with::
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

187183
Detecting 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
194193
call async code from both contexts – they have different semantics, esp.
195194
concerning 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

207209
First, 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

215217
a 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.

newsfragments/36.feature.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Major reorganization
2+
--------------------
3+
4+
The main entry point for calling asyncio from trio is now the
5+
:func:`trio_asyncio.aio_as_trio` adapter.
6+
7+
Instead of calling :func:`asyncio.get_event_loop`, directly access the
8+
contextvar ``trio_aio_loop`` (aka :var:`trio_asyncio.adapter.current_loop`).
9+

newsfragments/36.removal.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Deprecations
2+
------------
3+
4+
:func:`run_asyncio` is deprecated: replace with a :func:`aio_as_trio` wrapper.
5+
6+
:func:`trio2aio` is deprecated: replace with :func:`aio_as_trio`.
7+
8+
:func:`run_future` and :meth:`TrioEventLoop.run_future` are deprecated: replace with :func:`run_aio_future`.
9+
10+
:func:`run_coroutine` and :meth:`TrioEventLoop.run_coroutine` are deprecated: replace with :func:`run_aio_coroutine`.
11+
12+
:meth:`TrioEventLoop.wrap_generator` is deprecated: replace with a :func:`aio_as_trio` wrapper.
13+
14+
:meth:`TrioEventLoop.run_iterator` is deprecated: replace with :func:`aio_as_trio`.
15+

0 commit comments

Comments
 (0)