asyncio - how to stop background task cleanly

Marko Rauhamaa marko at pacujo.net
Sun Feb 7 02:53:09 EST 2016


"Frank Millman" <frank at chagford.com>:

> Alas, I spoke too soon.
>
> [...]
>
> 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.

I can't see your complete program, but here's mine, and it seems to be
working:

========================================================================
#!/usr/bin/env python3

import asyncio, time

def main():
    loop = asyncio.get_event_loop()
    try:
        task = asyncio.async(background_task())
        loop.run_until_complete(asyncio.wait([ task, terminator(task) ]))
    finally:
        loop.close()

@asyncio.coroutine
def terminator(task):
    yield from asyncio.sleep(5)
    task.cancel()
    yield from asyncio.wait([ task ])

@asyncio.coroutine
def background_task():
    try:
        while True:
            print('start')
            time.sleep(2)
            print('done')
            yield from asyncio.sleep(10)
    except asyncio.CancelledError:
            print('cleanup')
    print('DONE')

if __name__ == '__main__':
    main()
========================================================================

(My Python is slightly older, so replace

   yield from ==> await
   @asyncio.coroutine ==> async
   asyncio.async ==> asyncio.ensure_future)


Marko


More information about the Python-list mailing list