Python Glossary Русский

Python 3.4+until 3.16Async programmingfunction

asyncio.get_event_loop_policy()

event loop

Deprecated: returns the global event loop policy; deprecated since 3.14 with removal in 3.16. Pick the loop via loop_factory of asyncio.run().

Syntax

policy = asyncio.get_event_loop_policy()

A policy is a global object that decided which loop gets created and which counts as current; people swapped it to install uvloop or WindowsSelectorEventLoop. The whole policy system is deprecated in 3.14: instead of a global swap, pass the loop explicitly with asyncio.run(main(), loop_factory=...) (3.12+) or asyncio.Runner(loop_factory=...) (3.11+). Since 3.14 the call emits DeprecationWarning. The usual loop_factory is asyncio.EventLoop (3.13+), the best loop for the current platform.

Examples

# Python 3.11+
import asyncio
async def main():
    return type(asyncio.get_running_loop()).__name__
with asyncio.Runner(loop_factory=asyncio.new_event_loop) as runner:  # замена политике
    print(runner.run(main()).endswith('EventLoop'))  # → True
import asyncio
import warnings
with warnings.catch_warnings():
    warnings.simplefilter('ignore', DeprecationWarning)  # с 3.14 вызов устарел
    policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
print(loop.is_closed())  # → False
loop.close()
# Python 3.16+
import asyncio
async def main():
    return type(asyncio.get_running_loop()) is asyncio.EventLoop
# политик нет: цикл задают явно, на один запуск
print(asyncio.run(main(), loop_factory=asyncio.EventLoop))   # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy