Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102616

Re: asyncio - how to stop background task cleanly

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: asyncio - how to stop background task cleanly
Date Sun, 07 Feb 2016 09:53:09 +0200
Organization A noiseless patient Spider
Lines 65
Message-ID <87r3gpq7mi.fsf@elektro.pacujo.net> (permalink)
References <mailman.32.1454746369.2317.python-list@python.org> <87lh6ys052.fsf@elektro.pacujo.net> <mailman.41.1454767329.2317.python-list@python.org> <87fux5svhk.fsf@elektro.pacujo.net> <8737t5shhp.fsf@elektro.pacujo.net> <n96kjr$mvl$1@ger.gmane.org> <mailman.62.1454829054.2317.python-list@python.org>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="b7cb1518d23ec19d482dcc9c31d30fdd"; logging-data="15260"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19vGaWD9zS9ZqWfgBIkeJKZ"
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:jD8q4XE/KjBPcy8n7t1s13aypUU= sha1:QUgYCJzpaRSYR8jSeSE19u0MTbg=
Xref csiph.com comp.lang.python:102616

Show key headers only | View raw


"Frank Millman" <frank@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

asyncio - how to stop background task cleanly "Frank Millman" <frank@chagford.com> - 2016-02-06 09:55 +0200
  Re: asyncio - how to stop background task cleanly Marko Rauhamaa <marko@pacujo.net> - 2016-02-06 10:39 +0200
    Re: asyncio - how to stop background task cleanly "Frank Millman" <frank@chagford.com> - 2016-02-06 16:01 +0200
      Re: asyncio - how to stop background task cleanly Marko Rauhamaa <marko@pacujo.net> - 2016-02-06 17:34 +0200
        Re: asyncio - how to stop background task cleanly Marko Rauhamaa <marko@pacujo.net> - 2016-02-06 22:37 +0200
          Re: asyncio - how to stop background task cleanly "Frank Millman" <frank@chagford.com> - 2016-02-07 07:27 +0200
          Re: asyncio - how to stop background task cleanly "Frank Millman" <frank@chagford.com> - 2016-02-07 09:10 +0200
            Re: asyncio - how to stop background task cleanly Marko Rauhamaa <marko@pacujo.net> - 2016-02-07 09:53 +0200
              Re: asyncio - how to stop background task cleanly "Frank Millman" <frank@chagford.com> - 2016-02-07 10:55 +0200
                Re: asyncio - how to stop background task cleanly Marko Rauhamaa <marko@pacujo.net> - 2016-02-07 11:27 +0200
                Re: asyncio - how to stop background task cleanly "Frank Millman" <frank@chagford.com> - 2016-02-07 12:20 +0200

csiph-web