10000 bpo-31950: Improve event loop policy doc by pitrou · Pull Request #4306 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-31950: Improve event loop policy doc #4306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions Doc/library/asyncio-eventloops.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
--------------------------------

Expand All @@ -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())
0