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


Groups > comp.lang.python > #102583

Re: asyncio - how to stop background task cleanly

From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: asyncio - how to stop background task cleanly
Date 2016-02-06 16:01 +0200
Message-ID <mailman.41.1454767329.2317.python-list@python.org> (permalink)
References <mailman.32.1454746369.2317.python-list@python.org> <87lh6ys052.fsf@elektro.pacujo.net>

Show all headers | View raw


"Marko Rauhamaa"  wrote in message news:87lh6ys052.fsf@elektro.pacujo.net...
>
> "Frank Millman" <frank@chagford.com>:
>
> > When shutting the main program down, I want to stop the task, but I
> > cannot figure out how to stop it cleanly - i.e. wait until it has
> > finished the current task and possibly performed some cleanup, before
> > continuing.
>
> Here (and really, only here) is where asyncio shows its superiority over
> threads: you can multiplex.
>
> You should
>
>    await asyncio.wait(..., return_when=asyncio.FIRST_COMPLETED)
>
> to deal with multiple alternative stimuli.
>

Thanks, Marko, that works very well.

It took me a while to get it working, because I initiate shutting down the 
program from another thread. Eventually I figured out that I could put all 
my event loop shutdown procedures into a coroutine, and then call 
asyncio.run_coroutine_threadsafe() from the main thread.

Now I just have one problem left. I will keep experimenting, but if someone 
gives me a hint in the meantime it will be appreciated.

I run my background task like this -

    stop_task = False

    async def background_task():
        while not stop_task:
            await perform_task()
            await asyncio.sleep(10)

I stop the task by setting stop_task to True. It works, but it waits for the 
10-second sleep to expire before it is actioned.

With threading, I could set up a threading.Event(), call 
evt.wait(timeout=10) to run the loop, and evt.set() to stop it. It stopped 
instantly.

Is there an equivalent in asyncio?

Thanks

Frank

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