How to Easily Do Asynchronous Programming with Asyncio in Python
URL:
https://youtu.be/2IW-ZEui4h4Channel/Host:
ArjanCodesPublish Date:
2021.12.17Reviewed Date:
On This Day...
Example:
import asyncio
async def main():
await asyncio.sleep(0.5)
print('Bryan')
await asyncio.sleep(0.5)
asyncio.run(main())
#>>> Bryan
async
import asyncio
async def main():
print('Bryan')
asyncio.run(main())
await
import asyncio
async def main():
await asyncio.sleep(0.5)
print('Bryan')
await asyncio.sleep(0.5)
asyncio.run(main())
#>>> Bryan
gather
Run several functions in parallel
import asyncio
async def main0():
print('Bryan')
async def main1():
print('John')
async def main2():
print('Jane')
async def testing():
asyncio.gather(
main0(),
main1(),
main2()
)
asyncio.run(testing())
Structure parallelism and sequential execution elegantly
import asyncio
async def run_sequence(*functions: Awaitable[Any]) -> None:
for function in functions:
await function
async def run_parallel(*functions: Awaitable[Any]) -> None:
await asyncio.gather(*functions)
Backlinks