Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102574
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | "Frank Millman" <frank@chagford.com> |
| Newsgroups | comp.lang.python |
| Subject | asyncio - how to stop background task cleanly |
| Date | Sat, 6 Feb 2016 09:55:26 +0200 |
| Lines | 36 |
| Message-ID | <mailman.32.1454746369.2317.python-list@python.org> (permalink) |
| 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 cuAeOKmO+avl8HGyPbhtGw65NJnOIKQahQK/7DIUYgZQ== |
| 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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'finished.': 0.07; 'cleanup,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'timeout': 0.09; 'thread': 0.10; 'def': 0.13; 'async': 0.16; 'called,': 0.16; 'cleanly': 0.16; 'flag,': 0.16; 'ideal.': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'set()': 0.16; 'subject:stop': 0.16; 'true:': 0.16; 'finished': 0.23; 'previously': 0.24; 'header:X-Complaints-To:1': 0.26; 'figure': 0.27; 'thread,': 0.29; 'allows': 0.30; 'task': 0.30; 'seconds': 0.31; 'another': 0.32; 'possibly': 0.32; 'run': 0.33; 'i.e.': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'thanks': 0.37; 'received:org': 0.37; 'enough': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'skip:a 40': 0.64; 'frank': 0.72; 'await': 0.76; 'stop,': 0.84; 'task,': 0.91; 'instantly': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | 197.89.169.151 |
| 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:102574 |
Show key headers only | View raw
Hi all
It is easy enough to set up a task to run in the background every 10 seconds
using asyncio -
async def background_task():
while True:
await perform_task()
await asyncio.sleep(10)
asyncio.ensure_future(background_task())
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.
async def background_task():
await perform_setup()
while condition:
await perform_task()
await asyncio.sleep(10)
await perform_cleanup()
Previously I would run the task in another thread, then set a flag to tell
it to stop, then join() the thread which would block until the task had
finished. I used threading.Event as the flag, which allows it to 'sleep'
using wait() with a timeout value, but reacts instantly when set() is
called, so it was ideal.
Is there a way to achieve this using asyncio?
Thanks
Frank Millman
Back to comp.lang.python | Previous | Next — 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