blob: 100fff561c5b1a17428ea839da7d409910f5474b [file] [log] [blame]
Victor Stinnerdb39a0d2014-01-16 18:58:01 +01001.. currentmodule:: asyncio
2
Victor Stinner0f3e6bc2014-02-19 23:15:02 +01003.. _asyncio-dev:
4
Victor Stinnerdb39a0d2014-01-16 18:58:01 +01005Develop with asyncio
6====================
7
8Asynchronous programming is different than classical "sequential" programming.
Eli Bendersky679688e2014-01-20 08:13:31 -08009This page lists common traps and explains how to avoid them.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +010010
11
Victor Stinner62511fd2014-06-23 00:36:11 +020012.. _asyncio-debug-mode:
13
14Debug mode of asyncio
15---------------------
16
Andrew Svetlov49199072015-09-24 14:32:39 +030017The implementation of :mod:`asyncio` has been written for performance.
18In order to ease the development of asynchronous code, you may wish to
19enable *debug mode*.
Victor Stinnerd71dcbb2014-08-25 17:04:12 +020020
Andrew Svetlov49199072015-09-24 14:32:39 +030021To enable all debug checks for an application:
Victor Stinnerd71dcbb2014-08-25 17:04:12 +020022
Victor Stinner6a1b0042015-02-04 16:14:33 +010023* Enable the asyncio debug mode globally by setting the environment variable
Victor Stinner44862df2017-11-20 07:14:07 -080024 :envvar:`PYTHONASYNCIODEBUG` to ``1``, using ``-X dev`` command line option
25 (see the :option:`-X` option), or by calling
26 :meth:`AbstractEventLoop.set_debug`.
Victor Stinner6a1b0042015-02-04 16:14:33 +010027* Set the log level of the :ref:`asyncio logger <asyncio-logger>` to
28 :py:data:`logging.DEBUG`. For example, call
29 ``logging.basicConfig(level=logging.DEBUG)`` at startup.
30* Configure the :mod:`warnings` module to display :exc:`ResourceWarning`
31 warnings. For example, use the ``-Wdefault`` command line option of Python to
32 display them.
33
34Examples debug checks:
Victor Stinner62511fd2014-06-23 00:36:11 +020035
36* Log :ref:`coroutines defined but never "yielded from"
37 <asyncio-coroutine-not-scheduled>`
Guido van Rossumf68afd82016-08-08 09:41:21 -070038* :meth:`~AbstractEventLoop.call_soon` and :meth:`~AbstractEventLoop.call_at` methods
Victor Stinner62511fd2014-06-23 00:36:11 +020039 raise an exception if they are called from the wrong thread.
40* Log the execution time of the selector
41* Log callbacks taking more than 100 ms to be executed. The
Guido van Rossumf68afd82016-08-08 09:41:21 -070042 :attr:`AbstractEventLoop.slow_callback_duration` attribute is the minimum
Victor Stinner62511fd2014-06-23 00:36:11 +020043 duration in seconds of "slow" callbacks.
Victor Stinner6a1b0042015-02-04 16:14:33 +010044* :exc:`ResourceWarning` warnings are emitted when transports and event loops
45 are :ref:`not closed explicitly <asyncio-close-transports>`.
Victor Stinner62511fd2014-06-23 00:36:11 +020046
Victor Stinner44862df2017-11-20 07:14:07 -080047.. versionchanged:: 3.7
48
49 The new ``-X dev`` command line option can now also be used to enable
50 the debug mode.
51
Victor Stinner62511fd2014-06-23 00:36:11 +020052.. seealso::
53
Guido van Rossumf68afd82016-08-08 09:41:21 -070054 The :meth:`AbstractEventLoop.set_debug` method and the :ref:`asyncio logger
Victor Stinner62511fd2014-06-23 00:36:11 +020055 <asyncio-logger>`.
56
57
Victor Stinner1077dee2015-01-30 00:55:58 +010058Cancellation
59------------
60
61Cancellation of tasks is not common in classic programming. In asynchronous
Mike DePalatis87c3c5d2017-08-03 10:20:42 -040062programming, not only is it something common, but you have to prepare your
Victor Stinner1077dee2015-01-30 00:55:58 +010063code to handle it.
64
65Futures and tasks can be cancelled explicitly with their :meth:`Future.cancel`
66method. The :func:`wait_for` function cancels the waited task when the timeout
67occurs. There are many other cases where a task can be cancelled indirectly.
68
69Don't call :meth:`~Future.set_result` or :meth:`~Future.set_exception` method
70of :class:`Future` if the future is cancelled: it would fail with an exception.
71For example, write::
72
73 if not fut.cancelled():
74 fut.set_result('done')
75
76Don't schedule directly a call to the :meth:`~Future.set_result` or the
77:meth:`~Future.set_exception` method of a future with
Guido van Rossumf68afd82016-08-08 09:41:21 -070078:meth:`AbstractEventLoop.call_soon`: the future can be cancelled before its method
Victor Stinner1077dee2015-01-30 00:55:58 +010079is called.
80
81If you wait for a future, you should check early if the future was cancelled to
82avoid useless operations. Example::
83
Andrew Svetlov88743422017-12-11 17:35:49 +020084 async def slow_operation(fut):
Victor Stinner1077dee2015-01-30 00:55:58 +010085 if fut.cancelled():
86 return
87 # ... slow computation ...
Andrew Svetlov88743422017-12-11 17:35:49 +020088 await fut
Victor Stinner1077dee2015-01-30 00:55:58 +010089 # ...
90
91The :func:`shield` function can also be used to ignore cancellation.
92
93
Victor Stinner606ab032014-02-01 03:18:58 +010094.. _asyncio-multithreading:
95
96Concurrency and multithreading
97------------------------------
98
99An event loop runs in a thread and executes all callbacks and tasks in the same
Victor Stinner86516d92014-02-18 09:22:00 +0100100thread. While a task is running in the event loop, no other task is running in
Andrew Svetlov88743422017-12-11 17:35:49 +0200101the same thread. But when the task uses ``await``, the task is suspended
Victor Stinner5cb84ed2014-02-04 18:18:27 +0100102and the event loop executes the next task.
Victor Stinner606ab032014-02-01 03:18:58 +0100103
Victor Stinner5cb84ed2014-02-04 18:18:27 +0100104To schedule a callback from a different thread, the
Guido van Rossumf68afd82016-08-08 09:41:21 -0700105:meth:`AbstractEventLoop.call_soon_threadsafe` method should be used. Example::
Victor Stinner5cb84ed2014-02-04 18:18:27 +0100106
Guido van Rossum601953b2015-10-05 16:20:00 -0700107 loop.call_soon_threadsafe(callback, *args)
Victor Stinner606ab032014-02-01 03:18:58 +0100108
Victor Stinner790202d2014-02-07 19:03:05 +0100109Most asyncio objects are not thread safe. You should only worry if you access
110objects outside the event loop. For example, to cancel a future, don't call
111directly its :meth:`Future.cancel` method, but::
112
113 loop.call_soon_threadsafe(fut.cancel)
114
Victor Stinner606ab032014-02-01 03:18:58 +0100115To handle signals and to execute subprocesses, the event loop must be run in
116the main thread.
117
Guido van Rossum601953b2015-10-05 16:20:00 -0700118To schedule a coroutine object from a different thread, the
119:func:`run_coroutine_threadsafe` function should be used. It returns a
120:class:`concurrent.futures.Future` to access the result::
121
122 future = asyncio.run_coroutine_threadsafe(coro_func(), loop)
123 result = future.result(timeout) # Wait for the result with a timeout
124
Guido van Rossumf68afd82016-08-08 09:41:21 -0700125The :meth:`AbstractEventLoop.run_in_executor` method can be used with a thread pool
Victor Stinner606ab032014-02-01 03:18:58 +0100126executor to execute a callback in different thread to not block the thread of
127the event loop.
128
129.. seealso::
130
Zachary Ware5819cfa2015-01-06 00:40:43 -0600131 The :ref:`Synchronization primitives <asyncio-sync>` section describes ways
132 to synchronize tasks.
Victor Stinner606ab032014-02-01 03:18:58 +0100133
Victor Stinner399c59d2015-01-09 01:32:02 +0100134 The :ref:`Subprocess and threads <asyncio-subprocess-threads>` section lists
135 asyncio limitations to run subprocesses from different threads.
136
137
138
Victor Stinner606ab032014-02-01 03:18:58 +0100139
Victor Stinner45b27ed2014-02-01 02:36:43 +0100140.. _asyncio-handle-blocking:
141
Eli Benderskyb73c8332014-02-09 06:07:47 -0800142Handle blocking functions correctly
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100143-----------------------------------
144
145Blocking functions should not be called directly. For example, if a function
146blocks for 1 second, other tasks are delayed by 1 second which can have an
147important impact on reactivity.
148
149For networking and subprocesses, the :mod:`asyncio` module provides high-level
Victor Stinner9592edb2014-02-02 15:03:02 +0100150APIs like :ref:`protocols <asyncio-protocol>`.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100151
152An executor can be used to run a task in a different thread or even in a
153different process, to not block the thread of the event loop. See the
Guido van Rossumf68afd82016-08-08 09:41:21 -0700154:meth:`AbstractEventLoop.run_in_executor` method.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100155
Victor Stinner45b27ed2014-02-01 02:36:43 +0100156.. seealso::
157
158 The :ref:`Delayed calls <asyncio-delayed-calls>` section details how the
159 event loop handles time.
160
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100161
162.. _asyncio-logger:
163
Victor Stinner45b27ed2014-02-01 02:36:43 +0100164Logging
165-------
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100166
Victor Stinner45b27ed2014-02-01 02:36:43 +0100167The :mod:`asyncio` module logs information with the :mod:`logging` module in
168the logger ``'asyncio'``.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100169
Guido van Rossumba29a4f2016-10-13 13:56:40 -0700170The default log level for the :mod:`asyncio` module is :py:data:`logging.INFO`.
171For those not wanting such verbosity from :mod:`asyncio` the log level can
172be changed. For example, to change the level to :py:data:`logging.WARNING`:
173
174.. code-block:: none
175
176 logging.getLogger('asyncio').setLevel(logging.WARNING)
177
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100178
179.. _asyncio-coroutine-not-scheduled:
180
181Detect coroutine objects never scheduled
182----------------------------------------
183
Victor Stinner530ef2f2014-07-08 12:39:10 +0200184When a coroutine function is called and its result is not passed to
Guido van Rossumf68afd82016-08-08 09:41:21 -0700185:func:`ensure_future` or to the :meth:`AbstractEventLoop.create_task` method,
Yury Selivanov04356e12015-06-30 22:13:22 -0400186the execution of the coroutine object will never be scheduled which is
187probably a bug. :ref:`Enable the debug mode of asyncio <asyncio-debug-mode>`
188to :ref:`log a warning <asyncio-logger>` to detect it.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100189
190Example with the bug::
191
192 import asyncio
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100193
Andrew Svetlov88743422017-12-11 17:35:49 +0200194 async def test():
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100195 print("never scheduled")
196
197 test()
198
199Output in debug mode::
200
Victor Stinner530ef2f2014-07-08 12:39:10 +0200201 Coroutine test() at test.py:3 was never yielded from
202 Coroutine object created at (most recent call last):
203 File "test.py", line 7, in <module>
204 test()
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100205
Yury Selivanov04356e12015-06-30 22:13:22 -0400206The fix is to call the :func:`ensure_future` function or the
Guido van Rossumf68afd82016-08-08 09:41:21 -0700207:meth:`AbstractEventLoop.create_task` method with the coroutine object.
Victor Stinner530ef2f2014-07-08 12:39:10 +0200208
209.. seealso::
210
211 :ref:`Pending task destroyed <asyncio-pending-task-destroyed>`.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100212
213
Victor Stinner530ef2f2014-07-08 12:39:10 +0200214Detect exceptions never consumed
215--------------------------------
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100216
Ashley Cambaf8802d82017-11-25 00:39:39 +0100217Python usually calls :func:`sys.excepthook` on unhandled exceptions. If
Victor Stinner530ef2f2014-07-08 12:39:10 +0200218:meth:`Future.set_exception` is called, but the exception is never consumed,
Ashley Cambaf8802d82017-11-25 00:39:39 +0100219:func:`sys.excepthook` is not called. Instead, :ref:`a log is emitted
Victor Stinner530ef2f2014-07-08 12:39:10 +0200220<asyncio-logger>` when the future is deleted by the garbage collector, with the
221traceback where the exception was raised.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100222
223Example of unhandled exception::
224
225 import asyncio
226
227 @asyncio.coroutine
228 def bug():
229 raise Exception("not consumed")
230
231 loop = asyncio.get_event_loop()
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400232 asyncio.ensure_future(bug())
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100233 loop.run_forever()
Victor Stinnerb8064a82015-02-23 11:41:56 +0100234 loop.close()
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100235
236Output::
237
Victor Stinner530ef2f2014-07-08 12:39:10 +0200238 Task exception was never retrieved
Victor Stinnerab1c8532014-10-12 21:37:16 +0200239 future: <Task finished coro=<coro() done, defined at asyncio/coroutines.py:139> exception=Exception('not consumed',)>
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100240 Traceback (most recent call last):
Victor Stinnerab1c8532014-10-12 21:37:16 +0200241 File "asyncio/tasks.py", line 237, in _step
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100242 result = next(coro)
Victor Stinner530ef2f2014-07-08 12:39:10 +0200243 File "asyncio/coroutines.py", line 141, in coro
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100244 res = func(*args, **kw)
Victor Stinnerab1c8532014-10-12 21:37:16 +0200245 File "test.py", line 5, in bug
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100246 raise Exception("not consumed")
247 Exception: not consumed
248
Victor Stinner530ef2f2014-07-08 12:39:10 +0200249:ref:`Enable the debug mode of asyncio <asyncio-debug-mode>` to get the
Victor Stinnerab1c8532014-10-12 21:37:16 +0200250traceback where the task was created. Output in debug mode::
251
252 Task exception was never retrieved
253 future: <Task finished coro=<bug() done, defined at test.py:3> exception=Exception('not consumed',) created at test.py:8>
254 source_traceback: Object created at (most recent call last):
255 File "test.py", line 8, in <module>
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400256 asyncio.ensure_future(bug())
Victor Stinnerab1c8532014-10-12 21:37:16 +0200257 Traceback (most recent call last):
258 File "asyncio/tasks.py", line 237, in _step
259 result = next(coro)
260 File "asyncio/coroutines.py", line 79, in __next__
261 return next(self.gen)
262 File "asyncio/coroutines.py", line 141, in coro
263 res = func(*args, **kw)
264 File "test.py", line 5, in bug
265 raise Exception("not consumed")
266 Exception: not consumed
Victor Stinner530ef2f2014-07-08 12:39:10 +0200267
Zachary Ware5819cfa2015-01-06 00:40:43 -0600268There are different options to fix this issue. The first option is to chain the
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100269coroutine in another coroutine and use classic try/except::
270
Andrew Svetlov88743422017-12-11 17:35:49 +0200271 async def handle_exception():
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100272 try:
Andrew Svetlov88743422017-12-11 17:35:49 +0200273 await bug()
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100274 except Exception:
275 print("exception consumed")
276
277 loop = asyncio.get_event_loop()
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400278 asyncio.ensure_future(handle_exception())
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100279 loop.run_forever()
Victor Stinnerb8064a82015-02-23 11:41:56 +0100280 loop.close()
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100281
Guido van Rossumf68afd82016-08-08 09:41:21 -0700282Another option is to use the :meth:`AbstractEventLoop.run_until_complete`
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100283function::
284
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400285 task = asyncio.ensure_future(bug())
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100286 try:
287 loop.run_until_complete(task)
288 except Exception:
289 print("exception consumed")
290
Zachary Ware5819cfa2015-01-06 00:40:43 -0600291.. seealso::
292
293 The :meth:`Future.exception` method.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100294
295
Zachary Ware5819cfa2015-01-06 00:40:43 -0600296Chain coroutines correctly
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100297--------------------------
298
299When a coroutine function calls other coroutine functions and tasks, they
Andrew Svetlov88743422017-12-11 17:35:49 +0200300should be chained explicitly with ``await``. Otherwise, the execution is
Eli Bendersky679688e2014-01-20 08:13:31 -0800301not guaranteed to be sequential.
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100302
Eli Bendersky679688e2014-01-20 08:13:31 -0800303Example with different bugs using :func:`asyncio.sleep` to simulate slow
304operations::
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100305
306 import asyncio
307
Andrew Svetlov88743422017-12-11 17:35:49 +0200308 async def create():
309 await asyncio.sleep(3.0)
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100310 print("(1) create file")
311
Andrew Svetlov88743422017-12-11 17:35:49 +0200312 async def write():
313 await asyncio.sleep(1.0)
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100314 print("(2) write into file")
315
Andrew Svetlov88743422017-12-11 17:35:49 +0200316 async def close():
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100317 print("(3) close file")
318
Andrew Svetlov88743422017-12-11 17:35:49 +0200319 async def test():
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400320 asyncio.ensure_future(create())
321 asyncio.ensure_future(write())
322 asyncio.ensure_future(close())
Andrew Svetlov88743422017-12-11 17:35:49 +0200323 await asyncio.sleep(2.0)
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100324 loop.stop()
325
326 loop = asyncio.get_event_loop()
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400327 asyncio.ensure_future(test())
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100328 loop.run_forever()
329 print("Pending tasks at exit: %s" % asyncio.Task.all_tasks(loop))
Victor Stinnerf40c6632014-01-28 23:32:40 +0100330 loop.close()
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100331
Martin Panter1050d2d2016-07-26 11:18:21 +0200332Expected output:
333
334.. code-block:: none
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100335
336 (1) create file
337 (2) write into file
338 (3) close file
339 Pending tasks at exit: set()
340
Martin Panter1050d2d2016-07-26 11:18:21 +0200341Actual output:
342
343.. code-block:: none
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100344
345 (3) close file
346 (2) write into file
Victor Stinner530ef2f2014-07-08 12:39:10 +0200347 Pending tasks at exit: {<Task pending create() at test.py:7 wait_for=<Future pending cb=[Task._wakeup()]>>}
348 Task was destroyed but it is pending!
349 task: <Task pending create() done at test.py:5 wait_for=<Future pending cb=[Task._wakeup()]>>
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100350
351The loop stopped before the ``create()`` finished, ``close()`` has been called
352before ``write()``, whereas coroutine functions were called in this order:
353``create()``, ``write()``, ``close()``.
354
Andrew Svetlov88743422017-12-11 17:35:49 +0200355To fix the example, tasks must be marked with ``await``::
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100356
Andrew Svetlov88743422017-12-11 17:35:49 +0200357 async def test():
358 await asyncio.ensure_future(create())
359 await asyncio.ensure_future(write())
360 await asyncio.ensure_future(close())
361 await asyncio.sleep(2.0)
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100362 loop.stop()
363
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400364Or without ``asyncio.ensure_future()``::
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100365
Andrew Svetlov88743422017-12-11 17:35:49 +0200366 async def test():
367 await create()
368 await write()
369 await close()
370 await asyncio.sleep(2.0)
Victor Stinnerdb39a0d2014-01-16 18:58:01 +0100371 loop.stop()
372
Victor Stinner530ef2f2014-07-08 12:39:10 +0200373
374.. _asyncio-pending-task-destroyed:
375
376Pending task destroyed
377----------------------
378
379If a pending task is destroyed, the execution of its wrapped :ref:`coroutine
380<coroutine>` did not complete. It is probably a bug and so a warning is logged.
381
Martin Panter1050d2d2016-07-26 11:18:21 +0200382Example of log:
383
384.. code-block:: none
Victor Stinner530ef2f2014-07-08 12:39:10 +0200385
386 Task was destroyed but it is pending!
Victor Stinnerab1c8532014-10-12 21:37:16 +0200387 task: <Task pending coro=<kill_me() done, defined at test.py:5> wait_for=<Future pending cb=[Task._wakeup()]>>
Victor Stinner530ef2f2014-07-08 12:39:10 +0200388
389:ref:`Enable the debug mode of asyncio <asyncio-debug-mode>` to get the
Martin Panter1050d2d2016-07-26 11:18:21 +0200390traceback where the task was created. Example of log in debug mode:
391
392.. code-block:: none
Victor Stinnerab1c8532014-10-12 21:37:16 +0200393
394 Task was destroyed but it is pending!
395 source_traceback: Object created at (most recent call last):
396 File "test.py", line 15, in <module>
Yury Selivanovd7e19bb2015-05-11 16:33:41 -0400397 task = asyncio.ensure_future(coro, loop=loop)
Victor Stinnerab1c8532014-10-12 21:37:16 +0200398 task: <Task pending coro=<kill_me() done, defined at test.py:5> wait_for=<Future pending cb=[Task._wakeup()] created at test.py:7> created at test.py:15>
399
Victor Stinner530ef2f2014-07-08 12:39:10 +0200400
401.. seealso::
402
403 :ref:`Detect coroutine objects never scheduled <asyncio-coroutine-not-scheduled>`.
404
Victor Stinner6a1b0042015-02-04 16:14:33 +0100405.. _asyncio-close-transports:
Victor Stinner188f2c02015-01-30 01:35:14 +0100406
Victor Stinner6a1b0042015-02-04 16:14:33 +0100407Close transports and event loops
408--------------------------------
Victor Stinner188f2c02015-01-30 01:35:14 +0100409
410When a transport is no more needed, call its ``close()`` method to release
Victor Stinner6a1b0042015-02-04 16:14:33 +0100411resources. Event loops must also be closed explicitly.
Victor Stinner188f2c02015-01-30 01:35:14 +0100412
Victor Stinner6a1b0042015-02-04 16:14:33 +0100413If a transport or an event loop is not closed explicitly, a
414:exc:`ResourceWarning` warning will be emitted in its destructor. By default,
415:exc:`ResourceWarning` warnings are ignored. The :ref:`Debug mode of asyncio
416<asyncio-debug-mode>` section explains how to display them.