Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 606ab03

Browse files
committed
asyncio doc: add "Concurrency and multithreading" section
1 parent 45b27ed commit 606ab03

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

Doc/library/asyncio-dev.rst

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ Asynchronous programming is different than classical "sequential" programming.
77
This page lists common traps and explains how to avoid them.
88

99

10+
.. _asyncio-multithreading:
11+
12+
Concurrency and multithreading
13+
------------------------------
14+
15+
An event loop runs in a thread and executes all callbacks and tasks in the same
16+
thread. If a callback should be scheduled from a different thread, the
17+
:meth:`BaseEventLoop.call_soon_threadsafe` method should be used.
18+
19+
While a task in running in the event loop, no other task is running in the same
20+
thread. But when the task uses ``yield from``, the task is suspended and the
21+
event loop executes the next task.
22+
23+
To handle signals and to execute subprocesses, the event loop must be run in
24+
the main thread.
25+
26+
The :meth:`BaseEventLoop.run_in_executor` method can be used with a thread pool
27+
executor to execute a callback in different thread to not block the thread of
28+
the event loop.
29+
30+
.. seealso::
31+
32+
See the :ref:`Synchronization primitives <asyncio-sync>` section to
33+
synchronize tasks.
34+
35+
1036
.. _asyncio-handle-blocking:
1137

1238
Handle correctly blocking functions
@@ -21,7 +47,7 @@ APIs like :ref:`protocols <protocol>`.
2147

2248
An executor can be used to run a task in a different thread or even in a
2349
different process, to not block the thread of the event loop. See the
24-
:func:`BaseEventLoop.run_in_executor` function.
50+
:meth:`BaseEventLoop.run_in_executor` method.
2551

2652
.. seealso::
2753

@@ -213,5 +239,3 @@ Or without ``asyncio.async()``::
213239
yield from asyncio.sleep(2.0)
214240
loop.stop()
215241

216-
.. XXX: Document "poll xxx" log message?
217-

Doc/library/asyncio-sync.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.. currentmodule:: asyncio
2-
.. _sync:
2+
.. _asyncio-sync:
33

44
Synchronization primitives
55
==========================

0 commit comments

Comments
 (0)