Created
September 26, 2022 18:24
-
-
Save okanyenigun/229971986049f89cabf079837953b564 to your computer and use it in GitHub Desktop.
asyncio example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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