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


Groups > comp.lang.python > #102596

Re: asyncio - how to stop background task cleanly

From Marko Rauhamaa <marko@pacujo.net>
Newsgroups comp.lang.python
Subject Re: asyncio - how to stop background task cleanly
Date 2016-02-06 22:37 +0200
Organization A noiseless patient Spider
Message-ID <8737t5shhp.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>

Show all headers | View raw


Marko Rauhamaa <marko@pacujo.net>:

>     async def background_task(cancel_event):
>          while True:
>              await asyncio.wait(
>                  perform_task, cancel_event.wait,
>                  return_when=asyncio.FIRST_COMPETED)
>              if cancel_event_is_set()
>                  break
>              await asyncio.wait(
>                  cancel_event.wait, timeout=10,
>                  return_when=asyncio.FIRST_COMPETED)
>              if cancel_event_is_set()
>                  break

[Typo: cancel_event_is_set() ==> cancel_event.is_set().]

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)


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