Created
May 28, 2024 21:50
-
-
Save eecsmap/e7eac60f9eaab01143c94d141479f21d to your computer and use it in GitHub Desktop.
simple demo of interactive usage of asyncio
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
# demo interactive mode in asyncio | |
import asyncio | |
count = 10 | |
async def count_down(): | |
while True: | |
global count | |
print("main: ", count) | |
count -= 1 | |
await asyncio.sleep(1) | |
# interactive mode changes the count value | |
async def interactive(): | |
global count | |
for i in range(3): | |
# get input | |
value = await asyncio.get_event_loop().run_in_executor( | |
None, input, "Enter a number: " | |
) | |
count = int(value) | |
async def main(): | |
await asyncio.gather(count_down(), interactive()) | |
# asyncio.run(main()) | |
with asyncio.Runner(debug=True) as runner: | |
runner.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment