import asyncio
import selectors
selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)
18.5.2.4. Event loop policies and the default policy
Event loop management is abstracted with a policy pattern, to provide maximal
flexibility for custom platforms and frameworks. Throughout the execution of a
process, a single global policy object manages the event loops available to the
process based on the calling context. A policy is an object implementing the
AbstractEventLoopPolicy interface.
For most users of asyncio, policies never have to be dealt with
explicitly, since the default global policy is sufficient (see below).
The module-level functions
get_event_loop() and set_event_loop() provide convenient access to
event loops managed by the default policy.
18.5.2.5. Event loop policy interface
An event loop policy must implement the following interface:
class asyncio.AbstractEventLoopPolicy
Event loop policy.
get_event_loop()
Get the event loop for the current context.
Returns an event loop object implementing the AbstractEventLoop
interface.
Raises an exception in case no event loop has been set for the current
context and the current policy does not specify to create one. It must
never return None.
new_event_loop()
Create and return a new event loop object according to this policy’s
rules.
If there’s need to set this loop as the event loop for the current
context, 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 asyncio. If the current thread
doesn’t already have an event loop associated with it, the default policy’s
get_event_loop() method creates one when
called from the main thread, but raises RuntimeError otherwise.
18.5.2.6. Access to the global loop policy
asyncio.get_event_loop_policy()
Get the current event loop policy.
asyncio.set_event_loop_policy(policy)
Set the current event loop policy. If policy is None, the default
policy is restored.
18.5.2.7. Customizing the event loop policy
To implement a new event loop policy, it is recommended you subclass the
concrete default event loop policy DefaultEventLoopPolicy
and override the methods for which you want to change behavior, for example: