Issue
I am trying yo have a progress indicator where one task is doing something while the other indicates a progress. My hello world version of this things goes to sleep in one of the tasks but never wakes up.
What am I missing?
Thanks a lot
import asyncio
import sys
import time
import itertools
progress = True
def get_progress():
return progress
async def define_progress():
print("progress started")
await asyncio.sleep(2)
progress = False
print("progress ended")
async def run_spinner(msg):
spinner = itertools.cycle(['-', '/', '|', '\\'])
sys.stdout.write("{0} ".format(msg))
while(get_progress()):
sys.stdout.write("{0}".format(next(spinner)))
sys.stdout.flush()
time.sleep(0.3)
sys.stdout.write('\b')
async def main():
msg = "start logic"
await asyncio.gather(run_spinner(msg), define_progress())
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()
The output looks like below while the second line infinitely run the spinner.
progress started
creating package\
Solution
I had to make progress a global variable and add await asyncio.sleep(1) to run_spinner. I'm not sure this answers your question, but it does seem to do what you wanted.
import asyncio
import sys
import time
import itertools
global progress
progress = True
def get_progress():
global progress
return progress
async def define_progress():
global progress
print("progress started")
await asyncio.sleep(2)
progress = False
print("progress ended")
async def run_spinner(msg):
spinner = itertools.cycle(['-', '/', '|', '\\'])
sys.stdout.write("{0} ".format(msg))
while(get_progress()):
sys.stdout.write("{0}".format(next(spinner)))
sys.stdout.flush()
time.sleep(0.2)
sys.stdout.write('\b')
await asyncio.sleep(1)
async def main():
msg = "start logic"
await asyncio.gather(run_spinner(msg), define_progress())
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()
Answered By - REXXman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.