Nov. 19, 2018, 7:50 p.m.
Posted by soar

Python 3.7: asyncio.run()

Starting from Python 3.7 awaitable coroutines can be easily called with asyncio.run() method:

>>> import asyncio

>>> async def main():
...     print('hello')
...     await asyncio.sleep(1)
...     print('world')

>>> asyncio.run(main())
hello
world

As you remember, similar code in Python 3.6 looks like:

>>> import asyncio

>>> async def main():
...     print('hello')
...     await asyncio.sleep(1)
...     print('world')

>>> loop = asyncio.get_event_loop()
>>> loop.run_until_complete(main())
hello
world
>>> loop.close()

One more step to real asynchronous code.

Comments