Skip to main content
  1. Posts/

Python 3.7: asyncio.run()

·76 words·1 min· loading · loading ·
Development Python

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

 1>>> import asyncio
 2
 3>>> async def main():
 4...     print('hello')
 5...     await asyncio.sleep(1)
 6...     print('world')
 7
 8>>> asyncio.run(main())
 9hello
10world

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

 1>>> import asyncio
 2
 3>>> async def main():
 4...     print('hello')
 5...     await asyncio.sleep(1)
 6...     print('world')
 7
 8>>> loop = asyncio.get_event_loop()
 9>>> loop.run_until_complete(main())
10hello
11world
12>>> loop.close()

One more step to real asynchronous code.

@soar
Author
@soar
Senior SRE/DevOps engineer

Related

Miner Health-Check Script
·391 words·2 mins· loading · loading
Sysadmin Monitoring Python Windows
Wrapper for SGMiner to send health notifications
6-years old issue in Python CSV module
·419 words·2 mins· loading · loading
Development Python