Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102590
| From | Marko Rauhamaa <marko@pacujo.net> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: asyncio - how to stop background task cleanly |
| Date | 2016-02-06 17:34 +0200 |
| Organization | A noiseless patient Spider |
| Message-ID | <87fux5svhk.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> |
"Frank Millman" <frank@chagford.com>:
> "Marko Rauhamaa" wrote in message news:87lh6ys052.fsf@elektro.pacujo.net...
>> You should
>>
>> await asyncio.wait(..., return_when=asyncio.FIRST_COMPLETED)
>>
>> to deal with multiple alternative stimuli.
>>
>
> Thanks, Marko, that works very well.
>
> [...]
>
> 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)
You should set up a separate asyncio.Event or asyncio.Queue to send
"out-of-band" signals to your background_task:
async def background_task(cancel_event):
async def doze_off():
await asyncio.sleep(10)
while True:
await asyncio.wait(
perform_task, cancel_event.wait,
return_when=asyncio.FIRST_COMPETED)
if cancel_event_is_set()
break
await asyncio.wait(
doze_off, cancel_event.wait,
return_when=asyncio.FIRST_COMPETED)
if cancel_event_is_set()
break
That should be the idea, anyway. I didn't try it out.
> 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?
Yes, you could simplify the above thusly:
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
Marko
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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