diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2017-11-07 09:34:47 (GMT) |
---|---|---|
committer | Antoine Pitrou <pitrou@free.fr> | 2017-11-07 09:34:47 (GMT) |
commit | e65617f65e203a2a6d3e3100d0d6fed0ffa0613d (patch) | |
tree | e06912b26d0cfaabe4fc714220509e54fd13ddeb /Doc/library | |
parent | b0331c94c2a210d50e43d99b249ec83ee165e70c (diff) | |
download | cpython-e65617f65e203a2a6d3e3100d0d6fed0ffa0613d.zip cpython-e65617f65e203a2a6d3e3100d0d6fed0ffa0613d.tar.gz cpython-e65617f65e203a2a6d3e3100d0d6fed0ffa0613d.tar.bz2 |
bpo-31950: Improve event loop policy doc (GH-4306) (#4307)
(cherry picked from commit 4135c89395726024abddb7340a0c7a42c801f616)
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/asyncio-eventloops.rst | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/Doc/library/asyncio-eventloops.rst b/Doc/library/asyncio-eventloops.rst index d74fcb1..7970e90 100644 --- a/Doc/library/asyncio-eventloops.rst +++ b/Doc/library/asyncio-eventloops.rst @@ -148,10 +148,9 @@ process based on the calling context. A policy is an object implementing the :class:`AbstractEventLoopPolicy` interface. For most users of :mod:`asyncio`, policies never have to be dealt with -explicitly, since the default global policy is sufficient. +explicitly, since the default global policy is sufficient (see below). -The default policy defines context as the current thread, and manages an event -loop per thread that interacts with :mod:`asyncio`. The module-level functions +The module-level functions :func:`get_event_loop` and :func:`set_event_loop` provide convenient access to event loops managed by the default policy. @@ -189,6 +188,13 @@ An event loop policy must implement the following interface: context, :meth:`set_event_loop` must be called explicitly. +The default policy defines context as the current thread, and manages an event +loop per thread that interacts with :mod:`asyncio`. If the current thread +doesn't already have an event loop associated with it, the default policy's +:meth:`~AbstractEventLoopPolicy.get_event_loop` method creates one when +called from the main thread, but raises :exc:`RuntimeError` otherwise. + + Access to the global loop policy -------------------------------- @@ -200,3 +206,24 @@ Access to the global loop policy Set the current event loop policy. If *policy* is ``None``, the default policy is restored. + + +Customizing the event loop policy +--------------------------------- + +To implement a new event loop policy, it is recommended you subclass the +concrete default event loop policy :class:`DefaultEventLoopPolicy` +and override the methods for which you want to change behavior, for example:: + + class MyEventLoopPolicy(asyncio.DefaultEventLoopPolicy): + + def get_event_loop(self): + """Get the event loop. + + This may be None or an instance of EventLoop. + """ + loop = super().get_event_loop() + # Do something with loop ... + return loop + + asyncio.set_event_loop_policy(MyEventLoopPolicy()) |