Python Glossary Русский

Python 3.4+Async programmingfunction

asyncio.new_event_loop()

event loop

Creates a new event loop. It neither makes it current nor closes it; asyncio.run() or asyncio.Runner is usually the better choice.

Syntax

loop = asyncio.new_event_loop()

A new loop lives until you close it: without loop.close() file descriptors leak and a ResourceWarning follows. It does not become current, so get_event_loop() will not see it without set_event_loop(). asyncio.run() handles all of this: it creates, runs, finalizes async generators, shuts down the thread pool and closes the loop. A manual loop fits, for example, a long-lived loop in its own thread.

Examples

import asyncio
async def main():
    await asyncio.sleep(0.01)
    return 'готово'
loop = asyncio.new_event_loop()
try:
    print(loop.run_until_complete(main()))  # → готово
finally:
    loop.close()  # закрыть обязательно
print(loop.is_closed())  # → True

See also

Open in the full glossarydocs.python.orgReport an inaccuracy