asyncio - how to stop background task cleanly

Frank Millman frank at chagford.com
Sun Feb 7 02:10:36 EST 2016


"Frank Millman"  wrote in message news:n96kjr$mvl$1 at ger.gmane.org...
>
> "Marko Rauhamaa"  wrote in message 
> news:8737t5shhp.fsf at elektro.pacujo.net...
>
> > Actually, cancellation is specially supported in asyncio (<URL:
> > https://docs.python.org/3/library/asyncio-task.html#asyncio.Task.cancel>)
> > so this should do:
> >
> >     async def background_task():
> >         while True:
> >             await perform_task()
> >             await asyncio.sleep(10)
> >
>
> That's exactly what I needed - thanks, Marko
>
>     async def background_task()
>         try:
>             while True:
>                 await perform_task()
>                 await asyncio.sleep(10)
>         except asyncio.CancelledError:
>                 await perform_cleanup()
>
> At startup -
>
>     task = asyncio.ensure_future(background_task())
>
> At shutdown -
>
>     task.cancel()
>     await asyncio.wait([task])
>
> Works perfectly - thanks again.
>

Alas, I spoke too soon.

I tried to simulate what would happen if the background task was busy with a 
task when it was cancelled -

    async def background_task()
        try:
            while True:
                print('start')
                time.sleep(2)
                print('done')
                await asyncio.sleep(10)
        except asyncio.CancelledError:
                print('cleanup')
        print('DONE')

If I cancel after a pair of 'start/done' appear, the background task is in 
the 'asyncio.sleep' stage. The words 'cleanup' and 'DONE' appear instantly, 
and the program halts.

If I cancel after 'start', but before 'done', the background task is 
executing a task. There is a delay of up to 2 seconds, then the words 
'done',  'cleanup', and 'DONE' appear, but the program hangs. If I press 
Ctrl+C, I get a traceback from the threading module -

    line 1288, in _shutdown
        t.join()
    line 1054, in join
        self._wait_for_tstate_lock()
    line 1070, in _wait_for_tstate_lock
KeyboardInterrupt

So it is waiting for join() to complete. I will continue investigating, but 
will report it here to see if anyone can come up with an 
explanation/solution.

Thanks

Frank





More information about the Python-list mailing list