66Developing with asyncio
77=======================
88
9- Asynchronous programming is different from classical "sequential"
9+ Asynchronous programming is different from classic "sequential"
1010programming.
1111
1212This page lists common mistakes and traps and explains how
@@ -21,19 +21,17 @@ Debug Mode
2121By default asyncio runs in production mode. In order to ease
2222the development asyncio has a *debug mode *.
2323
24- To enable debugging for an application :
24+ There are several ways to enable asyncio debug mode :
2525
26- * Enable the debug mode globally by setting the environment variable
27- :envvar: `PYTHONASYNCIODEBUG ` to ``1 ``.
26+ * Setting the :envvar: `PYTHONASYNCIODEBUG ` environment variable to ``1 ``.
2827
29- * Alternatively, the debug mode can be enabled by using the ``-X dev ``
30- command line option for Python (see the :option: `-X ` option).
28+ * Using the :option: `-X ` ``dev `` Python command line option.
3129
32- * Yet another way to enable the debug mode is by calling
33- :meth: `loop.set_debug ` or by passing ``debug=True `` to
34- :func: `asyncio.run `.
30+ * Passing ``debug=True `` to :func: `asyncio.run `.
3531
36- In addition to enabling debug mode, consider also:
32+ * Calling :meth: `loop.set_debug `.
33+
34+ In addition to enabling the debug mode, consider also:
3735
3836* setting the log level of the :ref: `asyncio logger <asyncio-logger >` to
3937 :py:data: `logging.DEBUG `, for example the following snippet of code
@@ -43,25 +41,25 @@ In addition to enabling debug mode, consider also:
4341
4442* configuring the :mod: `warnings ` module to display
4543 :exc: `ResourceWarning ` warnings. One way of doing that is by
46- using the `` -Wdefault `` command line option.
44+ using the :option: ` -W ` `` default `` command line option.
4745
4846
49- In asyncio debug mode the following checks are performed :
47+ When the debug mode is enabled :
5048
51- * Log :ref: `coroutines that were not awaited
52- <asyncio-coroutine-not-scheduled>`; this mitigates the "forgotten
53- await" pitfall.
49+ * asyncio checks for :ref: `coroutines that were not awaited
50+ <asyncio-coroutine-not-scheduled>` and logs them ; this mitigates
51+ the "forgotten await" pitfall.
5452
5553* Many non-treadsafe asyncio APIs (such as :meth: `loop.call_soon ` and
5654 :meth: `loop.call_at ` methods) raise an exception if they are called
5755 from a wrong thread.
5856
59- * Log the execution time of the IO selector if it takes too long to
60- perform an IO operation.
57+ * The execution time of the I/O selector is logged if it takes too long to
58+ perform an I/O operation.
6159
62- * Log callbacks taking longer than 100 ms to be executed . The
63- :attr: `loop.slow_callback_duration ` attribute is the minimum
64- duration in seconds of "slow" callbacks .
60+ * Callbacks taking longer than 100ms are logged . The
61+ :attr: `loop.slow_callback_duration ` attribute can be used to set the
62+ minimum execution duration in seconds that is considered "slow".
6563
6664
6765.. _asyncio-multithreading :
@@ -134,20 +132,21 @@ Logging
134132asyncio uses the :mod: `logging ` module and all logging is performed
135133via the ``"asyncio" `` logger.
136134
137- The default log level is :py:data: `logging.INFO `, which can easily be
135+ The default log level is :py:data: `logging.INFO `, which can be easily
138136adjusted::
139137
140138 logging.getLogger("asyncio").setLevel(logging.WARNING)
141139
142140
143141.. _asyncio-coroutine-not-scheduled :
144142
145- Detect never awaited coroutines
143+ Detect never- awaited coroutines
146144===============================
147145
148- When a coroutine is called (e.g. ``coro() `` instead of ``await coro() ``)
149- the call is not wrapped with :meth: `asyncio.create_task `, the execution
150- of the coroutine object will never be scheduled. For example::
146+ When a coroutine function is called, but not awaited
147+ (e.g. ``coro() `` instead of ``await coro() ``)
148+ or the coroutine is not scheduled with :meth: `asyncio.create_task `, asyncio
149+ will emit a :exc: `RuntimeWarning `::
151150
152151 import asyncio
153152
@@ -184,8 +183,8 @@ The usual fix is to either await the coroutine or call the
184183 await test()
185184
186185
187- Detect never consumed exceptions
188- ================================
186+ Detect never-retrieved exceptions
187+ =================================
189188
190189If a :meth: `Future.set_exception ` is called but the Future object is
191190never awaited on, the exception would never be propagated to the
0 commit comments