asyncio — Asynchronous I/O


Hello World!

importasyncioasyncdefmain():print('Hello ...')awaitasyncio.sleep(1)print('... World!')asyncio.run(main())

asyncio is a library to writeconcurrent code usingtheasync/await syntax.

asyncio is used as a foundation for multiple Python asynchronousframeworks that provide high-performance network and web-servers,database connection libraries, distributed task queues, etc.

asyncio is often a perfect fit for IO-bound and high-levelstructured network code.

asyncio provides a set ofhigh-level APIs to:

Additionally, there arelow-level APIs forlibrary and framework developers to:

You can experiment with anasyncio concurrent context in the REPL:

$ python -m asyncioasyncio REPL ...Use "await" directly instead of "asyncio.run()".Type "help", "copyright", "credits" or "license" for more information.>>>importasyncio>>>awaitasyncio.sleep(10,result='hello')'hello'

Reference

Note

The source code for asyncio can be found inLib/asyncio/.