Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'practice,': 0.07; 'below).': 0.09; 'method,': 0.09; 'def': 0.12; 'suggest': 0.14; 'thread': 0.14; 'called.': 0.16; 'loops': 0.16; 'optional': 0.16; 'subject:stop': 0.16; 'thread,': 0.16; 'threads,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'all,': 0.19; 'bit': 0.19; 'trying': 0.19; 'input': 0.22; 'putting': 0.22; 'separate': 0.22; 'install': 0.23; 'keyboard': 0.24; 'skip:l 30': 0.24; 'skip:" 30': 0.26; 'this:': 0.26; 'second': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'probably': 0.32; 'run': 0.32; 'skip:t 40': 0.33; 'third': 0.33; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'interact': 0.36; 'subject:?': 0.36; 'should': 0.36; 'e.g.': 0.38; 'stopped': 0.38; 'to:addr:python- list': 0.38; 'issue': 0.38; 'hosted': 0.39; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'most': 0.60; "you're": 0.61; 'first': 0.61; "you'll": 0.62; "you've": 0.63; 'here': 0.66; 'close': 0.67; 'frank': 0.68; 'below.': 0.71; 'manner': 0.72; 'gray': 0.84; 'together,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=2q+1IO3TsUhyZSy/HDago7mwdhs80lN0qTV6zuwlmrY=; b=kujQ1zl/qfcEZANngY3eYIMW/HiG1hRD6IjBXwSpru5PmjPRbLSN7l95p41nA24oH2 PVcTXb8Vs0U86KN8zdeHnYUs2+Aj34DX5qGX28bpreTu2UMypn33bVasJYsgIhHn9/rr GE1r3bfFc5YuCAiBcij8M91WVFxmRwfDFMF8VJ/dg7T7u1HTEPVnpOwh3GVCmFKcGkkQ qrpdvk0ozVTaloUJQc1fD2LelLfEOwuH58Xlnseh3aTYGgg6grj68to9HxjE1wTDS9Nl RiHEbdtypzgtpZxWrTIcmBNRHo4X1DdJSiQFCQwlUF/5+m1t44r7c0Z1GazMLXDtPoD4 FV2A== X-Received: by 10.236.229.133 with SMTP id h5mr5919970yhq.64.1402495041054; Wed, 11 Jun 2014 06:57:21 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 11 Jun 2014 07:56:40 -0600 Subject: Re: asyncio - how to stop loop? To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1402495049 news.xs4all.nl 2912 [2001:888:2000:d::a6]:44248 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73173 On Wed, Jun 11, 2014 at 1:19 AM, Frank Millman wrote: > First attempt - same as before > > loop = asyncio.get_event_loop() > threading.Thread(target=loop.run_forever).start() > input('Press to stop') > loop.stop() > loop.close() Each event loop is hosted by a specific thread. In this case you're getting the event loop of the main thread and then trying to run it in a separate thread, which is not a good idea. You can run an event loop in a separate thread, but you should install a separate event loop for that thread if you do (and then when you interact with the loop, do so in a thread-safe manner -- see below). > Second attempt - move the keyboard input to a separate thread > > def stop_loop(): > input('Press to stop') > loop.stop() > loop.close() > > loop = asyncio.get_event_loop() > threading.Thread(target=stop_loop).start() > loop.run_forever() One issue here is that (like most event loop implementations) event loops are not thread-safe. To make a call to the event loop across threads, you should be using the call_soon_threadsafe method, e.g. "loop.call_soon_threadsafe(loop.stop)". You'll also want to make sure that the event loop has actually stopped before you call loop.close -- see below. > Third attempt - get the loop to close itself (cannot use in practice, but > see what happens) > > def stop_loop(): > loop.stop() > loop.close() > > loop = asyncio.get_event_loop() > loop.call_later(2, stop_loop) > loop.run_forever() I think what's happening here is that per the docs loop.close should not be called while the loop is running. You've called loop.stop but you're still inside a callback, which is a bit of a gray area. You probably don't need to call loop.close at all, but if you want to do so I suggest putting it after the run_forever call, so you can be certain the loop has stopped when it's called. Putting all that together, you should have something like this: def stop_loop(): input('Press to stop') loop.call_soon_threadsafe(loop.stop) loop = asyncio.get_event_loop() threading.Thread(target=stop_loop).start() loop.run_forever() loop.close() # optional