Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102574
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | asyncio - how to stop background task cleanly |
| Date | 2016-02-06 09:55 +0200 |
| Message-ID | <mailman.32.1454746369.2317.python-list@python.org> (permalink) |
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