Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73211

Re: asyncio - how to stop loop?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.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 <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.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'practice,': 0.07; 'below).': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'suggest': 0.14; 'wrote': 0.14; 'thread': 0.14; 'called.': 0.16; 'loops': 0.16; 'optional': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 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:X-Complaints-To:1': 0.27; 'appreciated.': 0.29; 'am,': 0.29; '(like': 0.30; 'getting': 0.31; 'probably': 0.32; 'run': 0.32; 'skip:t 40': 0.33; 'third': 0.33; 'received:co.za': 0.34; 'received:za': 0.34; 'something': 0.35; 'but': 0.35; 'interact': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'should': 0.36; 'clear': 0.37; '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; 'received:org': 0.40; '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; 'received:41': 0.70; 'below.': 0.71; 'manner': 0.72; 'gray': 0.84; 'together,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From "Frank Millman" <frank@chagford.com>
Subject Re: asyncio - how to stop loop?
Date Thu, 12 Jun 2014 10:00:44 +0200
References <ln8vt7$79r$1@ger.gmane.org> <CALwzidnv07Wba9WJ=Nuc0_v4mvudYAXWh6BgjvW0o1hF3ooWkg@mail.gmail.com>
X-Gmane-NNTP-Posting-Host 41-133-114-47.dsl.mweb.co.za
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.3790.4657
X-RFC2646 Format=Flowed; Original
X-MimeOLE Produced By Microsoft MimeOLE V6.00.3790.4913
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.11032.1402560058.18130.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402560058 news.xs4all.nl 2918 [2001:888:2000:d::a6]:48561
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:73211

Show key headers only | View raw


"Ian Kelly" <ian.g.kelly@gmail.com> wrote in message 
news:CALwzidnv07Wba9WJ=Nuc0_v4mvudYAXWh6BgjvW0o1hF3ooWkg@mail.gmail.com...
> On Wed, Jun 11, 2014 at 1:19 AM, Frank Millman <frank@chagford.com> wrote:
>> First attempt - same as before
>>
>>     loop = asyncio.get_event_loop()
>>     threading.Thread(target=loop.run_forever).start()
>>     input('Press <enter> 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 <enter> 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 <enter> 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

Thanks very much for the very clear explanation.

Your solution works perfectly.

Much appreciated.

Frank


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: asyncio - how to stop loop? "Frank Millman" <frank@chagford.com> - 2014-06-12 10:00 +0200

csiph-web