After quite some reading on asyncio (I'm totally noob about it), I've managed to write simple programs that do what I want them to do.
Yet, I have some dobuts regarding as_completed method: how should I handle SIGINT signal properly.
So, let there be the following snippet:
#as_completed_example.py
import asyncio
import tqdm
import datetime
import sys
import signal
import random
#--------------------------------------------------------------------------------------
async def heavy_load(i):
#tqdm.tqdm.write('#DEBUG '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' '+str(i))
await asyncio.sleep(random.random())
return None
#--------------------------------------------------------------------------------------
async def main():
length = int(sys.argv[1])
inputs = list(range(length))
pbar = tqdm.tqdm(total=len(inputs),position=0,leave=True,bar_format='#PROGRESS {desc}: {percentage:.3f}%|{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}')
tasks = [heavy_load(i) for i in inputs]
for future in asyncio.as_completed(tasks):
_ = await future
pbar.update(1)
pbar.refresh()
#---------------------------------------------------------------------------
def sigint_handler(signum,frame):
tqdm.tqdm.write('#INFO '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' user aborted execution!')
sys.exit(0)
#---------------------------------------------------------------------------
if(__name__=='__main__'):
signal.signal(signal.SIGINT,sigint_handler)
asyncio.run(main())
If you call it as python3 as_completed_example.py 10000 (large number), progress bar is stucked at 0.00% for quite some time, while asyncio.as_completed is preparing its input arguments. That's ok.
But, if you send a SIGINT while progressbar is stucked at 0.00%, you get plenty of unhandled exceptions.
How should I modify my code to be able to catch them?
Thanks in advance!!