Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: "Frank Millman" Newsgroups: comp.lang.python Subject: Re: asyncio - how to stop background task cleanly Date: Sun, 7 Feb 2016 10:55:58 +0200 Lines: 65 Message-ID: References: <87lh6ys052.fsf@elektro.pacujo.net> <87fux5svhk.fsf@elektro.pacujo.net> <8737t5shhp.fsf@elektro.pacujo.net> <87r3gpq7mi.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 keIqmDFa2XY+eqwuz9j/cwatCXnnkLhh1soPi+0caIqw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '__name__': 0.07; 'main()': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'bug': 0.10; 'thread': 0.10; 'def': 0.13; 'properly': 0.15; "'__main__':": 0.16; 'ctrl+c': 0.16; 'differs': 0.16; 'main():': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'somewhere.': 0.16; 'stripped': 0.16; 'subject:stop': 0.16; 'threading': 0.16; 'true:': 0.16; 'try:': 0.18; 'version.': 0.18; 'seems': 0.23; 'wrote': 0.23; 'thanks,': 0.24; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:X-Complaints-To:1': 0.26; 'external': 0.27; 'separate': 0.27; 'yield': 0.27; 'windows,': 0.29; 'program,': 0.29; 'task': 0.30; "can't": 0.32; 'run': 0.33; 'except': 0.34; 'quite': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:org': 0.37; 'wanted': 0.37; 'difference': 0.38; 'receipt': 0.38; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'press': 0.61; 'show': 0.62; 'genuine': 0.63; 'complete': 0.63; 'skip:a 40': 0.64; 'between': 0.65; 'here': 0.66; 'frank': 0.72; 'skip:n 40': 0.72; 'yours': 0.89; 'works!': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 197.89.92.138 In-Reply-To: <87r3gpq7mi.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102619 "Marko Rauhamaa" wrote in message news:87r3gpq7mi.fsf@elektro.pacujo.net... > > I can't see your complete program, but here's mine, and it seems to be > working > Thanks, Marko, I really appreciate your assistance. I wanted to show you my complete program, but as it is quite long I distilled it down to its essence, and lo and behold it works! So now I just have to go through my program and find where it differs - I must have a bug somewhere. For the record, here is my stripped down version. The main difference from yours is that I want to run a genuine 'loop forever', and only shut it down on receipt of some external signal. I have never been able to get Ctrl+C to work properly on Windows, so I use a separate thread that simply waits for Enter. You will see that if you press Enter after 'done' appears, the program closes instantly, but if you press it in between 'start' and 'done', it waits for the task to complete before it closes. Frank ============================================================= import asyncio, time import threading def main(): loop = asyncio.get_event_loop() task = asyncio.async(background_task()) threading.Thread(target=stop, args=(loop, task)).start() loop.run_forever() @asyncio.coroutine def background_task(): try: while True: print('start') time.sleep(2) print('done') yield from asyncio.sleep(5) except asyncio.CancelledError: print('cleanup') print('DONE') @asyncio.coroutine def shutdown(loop, task): task.cancel() yield from asyncio.wait([task], loop=loop) loop.stop() def stop(loop, task): input('Press to stop\n') asyncio.run_coroutine_threadsafe(shutdown(loop, task), loop) if __name__ == '__main__': main()