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


Groups > comp.lang.python > #60259

Re: Periodic execution with asyncio

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(using': 0.07; 'callback': 0.09; 'pep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'solution,': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'above?': 0.16; "guido's": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'tasks,': 0.16; 'true:': 0.16; 'write.': 0.16; 'wrote:': 0.18; 'version.': 0.19; 'import': 0.22; 'header:User- Agent:1': 0.23; 'versions': 0.24; 'second': 0.26; '(for': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'point': 0.28; 'code': 0.31; 'easier': 0.31; 'interface': 0.32; 'quite': 0.32; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'version': 0.36; 'yield': 0.36; 'similar': 0.36; 'should': 0.36; 'too': 0.37; 'implement': 0.38; 'initially': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'received:173': 0.61; 'kind': 0.63; 'more': 0.64; '7:00': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Periodic execution with asyncio
Date Fri, 22 Nov 2013 20:33:03 -0500
References <528FCCF2.4020608@tobix.eu> <l6or6b$aao$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-254-207.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.1
In-Reply-To <l6or6b$aao$1@ger.gmane.org>
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.3063.1385170405.18130.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385170405 news.xs4all.nl 15977 [2001:888:2000:d::a6]:45655
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60259

Show key headers only | View raw


On 11/22/2013 7:00 PM, Terry Reedy wrote:
> On 11/22/2013 4:30 PM, Tobias M. wrote:

[snip callback versions by Tobias and me]

>> 2. How would you implement the second approach from the PEP (using a
>> coroutine) with the same interface as my PeriodicTask above?
>
> Theoretically, by Guido's rationale, that should be easier (for me, at
> least), since it would be more like the kind of Python code I already
> write.
>
>> I tried the second approach but wasn't able to come up with a solution,
>> as I was too confused by the concepts of coroutines, Tasks, etc.
>
> I will try to look at the PEP and see how it works for me.

I was initially baffled also until I managed to assemble all the needed 
pieces.

import asyncio

def f():
     print('Hello World')

@asyncio.coroutine
def g(func, interval):
     while True:
         yield from asyncio.sleep(interval)
         func()

loop = asyncio.get_event_loop()

try:
     loop.run_until_complete(asyncio.Task(g(f, 2)))
except KeyboardInterrupt:
     print('Loop stopped')

I think Guido's point is that async generator version is quite similar 
to the 'normal' synchronous version.

import time

def f():
     print('Hello World')

def h(func, interval):
     while True:
         time.sleep(interval)
         func()

try:
     h(f, 2))
except KeyboardInterrupt:
     print('Loop stopped')

-- 
Terry Jan Reedy

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


Thread

Re: Periodic execution with asyncio Terry Reedy <tjreedy@udel.edu> - 2013-11-22 20:33 -0500

csiph-web