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

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: asyncio - how to stop background task cleanly
Date Sat, 6 Feb 2016 16:01:49 +0200
Lines 52
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>
Mime-Version 1.0
Content-Type text/plain; format=flowed; charset="iso-8859-1"; reply-type=original
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de lTcgqQF5/Wzs7jqFzR34awE9h81m0cap6XvrfpU0SZHA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'cleanup,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'async': 0.16; 'cleanly': 0.16; 'evt.set()': 0.16; 'hint': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:stop': 0.16; 'thread.': 0.16; 'threading,': 0.16; 'threads:': 0.16; 'wrote': 0.23; 'finished': 0.23; 'thanks,': 0.24; 'header:In-Reply-To:1': 0.24; 'header:X-Complaints-To:1': 0.26; 'appreciated.': 0.27; 'figure': 0.27; 'equivalent': 0.27; 'figured': 0.29; 'initiate': 0.29; 'loop,': 0.29; 'really,': 0.29; 'sleep': 0.29; 'task': 0.30; 'another': 0.32; 'possibly': 0.32; 'run': 0.33; 'problem': 0.33; 'true.': 0.33; 'gives': 0.35; 'could': 0.35; 'false': 0.35; 'i.e.': 0.35; 'stopped': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; '(and': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'setting': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'someone': 0.38; 'skip:e 20': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'here': 0.66; 'frank': 0.72; 'skip:n 40': 0.72; 'await': 0.76; 'stimuli.': 0.84; 'superiority': 0.84; 'working,': 0.84; 'instantly.': 0.91; 'task,': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 197.89.169.151
In-Reply-To <87lh6ys052.fsf@elektro.pacujo.net>
X-MSMail-Priority Normal
Importance Normal
X-Newsreader Microsoft Windows Live Mail 15.4.3502.922
X-MimeOLE Produced By Microsoft MimeOLE V15.4.3502.922
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102583

Show key headers only | 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