Skip to content

Instantly share code, notes, and snippets.

@okanyenigun
Created September 26, 2022 18:24
Show Gist options
  • Save okanyenigun/229971986049f89cabf079837953b564 to your computer and use it in GitHub Desktop.
Save okanyenigun/229971986049f89cabf079837953b564 to your computer and use it in GitHub Desktop.
asyncio example
import asyncio
async def get_data_from_db():
print("connecting to database...")
await asyncio.sleep(3)
print('data is retrieved.')
return {'data': 'myData'}
async def counter():
for i in range(20):
print("In counter: ", i)
await asyncio.sleep(0.2)
async def main():
t1 = asyncio.create_task(get_data_from_db())
t2 = asyncio.create_task(counter())
value = await t1
print("The value is: ",value)
await t2
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment