Python - asyncio
- only one task runs at a time
- other tasks proceed when first function is asleep, waiting or done
Just using sleep, not really asynchronous:
import asyncio
async def fn():
print('This is ')
await asyncio.sleep(1)
print('asynchronous programming')
await asyncio.sleep(1)
print('and not multi-threading')
asyncio.run(fn())
just using sleep, not really asynchronous:
import asyncio
async def fn():
print("one")
await asyncio.sleep(1)
await fn2()
print('four')
await asyncio.sleep(1)
print('five')
await asyncio.sleep(1)
async def fn2():
await asyncio.sleep(1)
print("two")
await asyncio.sleep(1)
print("three")
asyncio.run(fn())
task is run once there is time ( while main is sleeping ):
import asyncio
async def fn():
task=asyncio.create_task(fn2())
print("one")
#await asyncio.sleep(1)
#await fn2()
print('four')
await asyncio.sleep(1)
print('five')
await asyncio.sleep(1)
async def fn2():
#await asyncio.sleep(1)
print("two")
await asyncio.sleep(1)
print("three")
asyncio.run(fn())
Parallel tasks ( could be used for IO ):
import asyncio
async def func1():
print("Function 1 started..")
await asyncio.sleep(2) # sleep OR do IO instead
print("Function 1 Ended")
async def func2():
print("Function 2 started..")
await asyncio.sleep(3)
print("Function 2 Ended")
async def func3():
print("Function 3 started..")
await asyncio.sleep(1)
print("Function 3 Ended")
async def main():
L = await asyncio.gather(
func1(),
func2(),
func3(),
)
print("Main Ended..")
asyncio.run(main())