Skip to content

Instantly share code, notes, and snippets.

@eecsmap
Created May 28, 2024 21:50
Show Gist options
  • Save eecsmap/e7eac60f9eaab01143c94d141479f21d to your computer and use it in GitHub Desktop.
Save eecsmap/e7eac60f9eaab01143c94d141479f21d to your computer and use it in GitHub Desktop.
simple demo of interactive usage of asyncio
# 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