Debian Codegraph @

Doc/library/asyncio-dev.rst

raw
1 .. currentmodule:: asyncio
2
3 .. _asyncio-dev:
4
5 =======================
6 Developing with asyncio
7 =======================
8
9 Asynchronous programming is different from classic "sequential"
10 programming.
11
12 This page lists common mistakes and traps and explains how
13 to avoid them.
14
15
16 .. _asyncio-debug-mode:
17
18 Debug Mode
19 ==========
20
21 By default asyncio runs in production mode. In order to ease
22 the development asyncio has a *debug mode*.
23
24 There are several ways to enable asyncio debug mode:
25
26 * Setting the :envvar:`PYTHONASYNCIODEBUG` environment variable to ``1``.
27
28 * Using the :ref:`Python Development Mode <devmode>`.
29
30 * Passing ``debug=True`` to :func:`asyncio.run`.
31
32 * Calling :meth:`loop.set_debug`.
33
34 In addition to enabling the debug mode, consider also:
35
36 * setting the log level of the :ref:`asyncio logger <asyncio-logger>` to
37 :py:const:`logging.DEBUG`, for example the following snippet of code
38 can be run at startup of the application::
39
40 logging.basicConfig(level=logging.DEBUG)
41
42 * configuring the :mod:`warnings` module to display
43 :exc:`ResourceWarning` warnings. One way of doing that is by
44 using the :option:`-W` ``default`` command line option.
45
46
47 When the debug mode is enabled:
48
49 * Many non-threadsafe asyncio APIs (such as :meth:`loop.call_soon` and
50 :meth:`loop.call_at` methods) raise an exception if they are called
51 from a wrong thread.
52
53 * The execution time of the I/O selector is logged if it takes too long to
54 perform an I/O operation.
55
56 * Callbacks taking longer than 100 milliseconds are logged. The
57 :attr:`loop.slow_callback_duration` attribute can be used to set the
58 minimum execution duration in seconds that is considered "slow".
59
60
61 .. _asyncio-multithreading:
62
63 Concurrency and Multithreading
64 ==============================
65
66 An event loop runs in a thread (typically the main thread) and executes
67 all callbacks and Tasks in its thread. While a Task is running in the
68 event loop, no other Tasks can run in the same thread. When a Task
69 executes an ``await`` expression, the running Task gets suspended, and
70 the event loop executes the next Task.
71
72 To schedule a :term:`callback` from another OS thread, the
73 :meth:`loop.call_soon_threadsafe` method should be used. Example::
74
75 loop.call_soon_threadsafe(callback, *args)
76
77 Almost all asyncio objects are not thread safe, which is typically
78 not a problem unless there is code that works with them from outside
79 of a Task or a callback. If there's a need for such code to call a
80 low-level asyncio API, the :meth:`loop.call_soon_threadsafe` method
81 should be used, e.g.::
82
83 loop.call_soon_threadsafe(fut.cancel)
84
85 To schedule a coroutine object from a different OS thread, the
86 :func:`run_coroutine_threadsafe` function should be used. It returns a
87 :class:`concurrent.futures.Future` to access the result::
88
89 async def coro_func():
90 return await asyncio.sleep(1, 42)
91
92 # Later in another OS thread:
93
94 future = asyncio.run_coroutine_threadsafe(coro_func(), loop)
95 # Wait for the result:
96 result = future.result()
97
98 To handle signals the event loop must be
99 run in the main thread.
100
101 The :meth:`loop.run_in_executor` method can be used with a
102 :class:`concurrent.futures.ThreadPoolExecutor` to execute
103 blocking code in a different OS thread without blocking the OS thread
104 that the event loop runs in.
105
106 There is currently no way to schedule coroutines or callbacks directly
107 from a different process (such as one started with
108 :mod:`multiprocessing`). The :ref:`asyncio-event-loop-methods`
109 section lists APIs that can read from pipes and watch file descriptors
110 without blocking the event loop. In addition, asyncio's
111 :ref:`Subprocess <asyncio-subprocess>` APIs provide a way to start a
112 process and communicate with it from the event loop. Lastly, the
113 aforementioned :meth:`loop.run_in_executor` method can also be used
114 with a :class:`concurrent.futures.ProcessPoolExecutor` to execute
115 code in a different process.
116
117 .. _asyncio-handle-blocking:
118
119 Running Blocking Code
120 =====================
121
122 Blocking (CPU-bound) code should not be called directly. For example,
123 if a function performs a CPU-intensive calculation for 1 second,
124 all concurrent asyncio Tasks and IO operations would be delayed
125 by 1 second.
126
127 An executor can be used to run a task in a different thread or even in
128 a different process to avoid blocking the OS thread with the
129 event loop. See the :meth:`loop.run_in_executor` method for more
130 details.
131
132
133 .. _asyncio-logger:
134
135 Logging
136 =======
137
138 asyncio uses the :mod:`logging` module and all logging is performed
139 via the ``"asyncio"`` logger.
140
141 The default log level is :py:const:`logging.INFO`, which can be easily
142 adjusted::
143
144 logging.getLogger("asyncio").setLevel(logging.WARNING)
145
146
147 Network logging can block the event loop. It is recommended to use
148 a separate thread for handling logs or use non-blocking IO. For example,
149 see :ref:`blocking-handlers`.
150
151
152 .. _asyncio-coroutine-not-scheduled:
153
154 Detect never-awaited coroutines
155 ===============================
156
157 When a coroutine function is called, but not awaited
158 (e.g. ``coro()`` instead of ``await coro()``)
159 or the coroutine is not scheduled with :meth:`asyncio.create_task`, asyncio
160 will emit a :exc:`RuntimeWarning`::
161
162 import asyncio
163
164 async def test():
165 print("never scheduled")
166
167 async def main():
168 test()
169
170 asyncio.run(main())
171
172 Output::
173
174 test.py:7: RuntimeWarning: coroutine 'test' was never awaited
175 test()
176
177 Output in debug mode::
178
179 test.py:7: RuntimeWarning: coroutine 'test' was never awaited
180 Coroutine created at (most recent call last)
181 File "../t.py", line 9, in <module>
182 asyncio.run(main(), debug=True)
183
184 < .. >
185
186 File "../t.py", line 7, in main
187 test()
188 test()
189
190 The usual fix is to either await the coroutine or call the
191 :meth:`asyncio.create_task` function::
192
193 async def main():
194 await test()
195
196
197 Detect never-retrieved exceptions
198 =================================
199
200 If a :meth:`Future.set_exception` is called but the Future object is
201 never awaited on, the exception would never be propagated to the
202 user code. In this case, asyncio would emit a log message when the
203 Future object is garbage collected.
204
205 Example of an unhandled exception::
206
207 import asyncio
208
209 async def bug():
210 raise Exception("not consumed")
211
212 async def main():
213 asyncio.create_task(bug())
214
215 asyncio.run(main())
216
217 Output::
218
219 Task exception was never retrieved
220 future: <Task finished coro=<bug() done, defined at test.py:3>
221 exception=Exception('not consumed')>
222
223 Traceback (most recent call last):
224 File "test.py", line 4, in bug
225 raise Exception("not consumed")
226 Exception: not consumed
227
228 :ref:`Enable the debug mode <asyncio-debug-mode>` to get the
229 traceback where the task was created::
230
231 asyncio.run(main(), debug=True)
232
233 Output in debug mode::
234
235 Task exception was never retrieved
236 future: <Task finished coro=<bug() done, defined at test.py:3>
237 exception=Exception('not consumed') created at asyncio/tasks.py:321>
238
239 source_traceback: Object created at (most recent call last):
240 File "../t.py", line 9, in <module>
241 asyncio.run(main(), debug=True)
242
243 < .. >
244
245 Traceback (most recent call last):
246 File "../t.py", line 4, in bug
247 raise Exception("not consumed")
248 Exception: not consumed