Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'skip:l 60': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'callback': 0.09; 'exit': 0.09; 'main()': 0.09; 'def': 0.12; './python': 0.16; '23,': 0.16; '>in': 0.16; '>on': 0.16; '>the': 0.16; 'block.': 0.16; 'constructor.': 0.16; 'func': 0.16; 'loop.': 0.16; 'main():': 0.16; 'received:188.40': 0.16; 'received:188.40.28': 0.16; 'received:your-server.de': 0.16; 'spawn': 0.16; 'true:': 0.16; 'url:py': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'putting': 0.22; 'separate': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'script.': 0.24; 'server.': 0.24; 'class.': 0.26; 'task': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'generally': 0.29; '+0100,': 0.31; 'received:78.46': 0.31; 'class': 0.32; 'probably': 0.32; 'run': 0.32; 'running': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'subject:with': 0.35; 'created': 0.35; 'something': 0.35; 'but': 0.35; 'version': 0.36; 'skip:> 10': 0.36; 'yield': 0.36; 'done': 0.36; 'method': 0.36; 'thanks': 0.36; 'similar': 0.36; 'should': 0.36; 'two': 0.37; 'level': 0.37; 'nov': 0.38; 'tasks': 0.38; 'version,': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'skip:a 30': 0.61; "you'll": 0.62; 'name': 0.63; 'our': 0.64; 'more': 0.64; '20,': 0.68; 'skip:a 40': 0.72; ':).': 0.84; 'indefinitely': 0.84; 'phil': 0.84; 'skip:s 80': 0.84; 'toy': 0.84; 'understand!': 0.84; '2013': 0.98 User-Agent: K-9 Mail for Android In-Reply-To: <20131125105254.GA9462@phconnel-ws.cisco.com> References: <528FCCF2.4020608@tobix.eu> <52911065.6080308@tobix.eu> <20131125105254.GA9462@phconnel-ws.cisco.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----NID8OU5ZJYQL8H56JZ8AQE1H9S10OE" Subject: Re: Periodic execution with asyncio From: "Tobias M." Date: Tue, 26 Nov 2013 17:35:26 +0100 To: python-list@python.org X-Authenticated-Sender: tm@tobix.eu X-Virus-Scanned: Clear (ClamAV 0.97.8/18164/Tue Nov 26 18:47:14 2013) 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: 176 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385495552 news.xs4all.nl 15937 [2001:888:2000:d::a6]:45695 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60538 ------NID8OU5ZJYQL8H56JZ8AQE1H9S10OE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Thanks Phil, now I understand! I wasn't aware of the fact that tasks are au= tomatically attached to the event loop when they are created via their cons= tructor=2E I thought I have to pass them to a run_* method explicitly=2E = Phil Connell schrieb: >On Sat, Nov 23, 2013 at 09:= 30:29PM +0100, Tobias M=2E wrote: >> Now putting this into a PeriodicTask c= lass that provides a similar >interface >> like our callback version, I get= : >> >> >> import asyncio >> >> class PeriodicTask2(object): >> >> = def __init__(self, func, interval): >> self=2Efunc =3D func >> = self=2Einterval =3D interval >> self=2E_loop =3D asyncio=2E= get_event_loop() >> >> def start(self): >> self=2Eloop=2Erun= _until_complete(asyncio=2ETask(self=2E_run())) >> >> @asyncio=2Ecorout= ine >> def _run(self): >> while True: >> yiel= d from asyncio=2Esleep(self=2Einterval) >> self=2Efunc() >>= >> >> I don't know if I misunderstood anything, but as a user of this cl= ass >I am >> not able to run two task simultaneously because start() will b= lock=2E >In the >> callback version I could instanciate two PeriodicTasks a= nd run them >both at >> the same time (after a call to loop=2Erun_forever()= ), which is actually >what I >> wanted to achieve with this class=2E > >You= need to separate out creating tasks from running the event loop=2E > >Spec= ifically, you should run the event loop in exactly one place, >probably at = >the top level of your script=2E > > >A toy example: > >import asyncio > >@= asyncio=2Ecoroutine >def adder(*args, delay): > yield from asyncio=2Esle= ep(delay) > print(sum(args)) > >def main(): > asyncio=2ETask(adder(1,= 2, 3, delay=3D5)) > asyncio=2ETask(adder(10, 20, delay=3D3)) > > loo= p =3D asyncio=2Eget_event_loop() > loop=2Erun_forever() > >if __name__ = =3D=3D "__main__": > main() > > >$ =2E/python test=2Epy >30 >6 > > >Note= that run_forever() will block indefinitely (as the name suggests >:)=2E Th= is >is generally what you'll want for a long-running server=2E > >If you wa= nt do something more like: > - Spawn some tasks > - Run the event loo= p until they're done > - Exit > >then you'll need to use loop=2Erun_unti= l_complete(asyncio=2Egather(*tasks)) >instead=2E > > >HTH, >Phil ------NID8OU5ZJYQL8H56JZ8AQE1H9S10OE Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable Thanks Phil, now I understand!
I wasn't aw= are of the fact that tasks are automatically attached to the event loop whe= n they are created via their constructor=2E I thought I have to pass them t= o a run_* method explicitly=2E



=
Phil Connell <pconnell@gmail=2Ecom> schrieb:
On Sat, Nov =
23, 2013 at 09:30:29PM +0100, Tobias M=2E wrote:
Now putting this into a PeriodicTask class that= provides a similar interface
like our callback version, I get:
<= br />
import asyncio

class PeriodicTask2(object):

def __init__(self, func, interval):
self=2Efunc =3D func
self= =2Einterval =3D interval
self=2E_loop =3D asyncio=2Eget_event_loop()
def start(self):
self=2Eloop=2Erun_until_complete(asyncio=2E= Task(self=2E_run()))

@asyncio=2Ecoroutine
def _run(self):while True:
yield from asyncio=2Esleep(self=2Einterval)
self= =2Efunc()


I don't know if I misunderstood anything, but as= a user of this class I am
not able to run two task simultaneously bec= ause start() will block=2E In the
callback version I could instanciate= two PeriodicTasks and run them both at
the same time (after a call to= loop=2Erun_forever()), which is actually what I
wanted to achieve wit= h this class=2E

You need to separate out creating tasks f= rom running the event loop=2E

Specifically, you should run the e= vent loop in exactly one place, probably at
the top level of your scri= pt=2E


A toy example:

import asyncio

= @asyncio=2Ecoroutine
def adder(*args, delay):
yield from asyncio= =2Esleep(delay)
print(sum(args))

def main():
asyncio= =2ETask(adder(1, 2, 3, delay=3D5))
asyncio=2ETask(adder(10, 20, delay= =3D3))

loop =3D asyncio=2Eget_event_loop()
loop=2Erun_forev= er()

if __name__ =3D=3D "__main__":
main()


$ =2E/python test=2Epy
30
6


Note that run_forever() will block indefinitely (as the name = suggests :)=2E This
is generally what you'll want for a long-running s= erver=2E

If you want do something more like:
- Spawn some t= asks
- Run the event loop until they're done
- Exit

th= en you'll need to use loop=2Erun_until_complete(asyncio=2Egather(*tasks))instead=2E


HTH,
Phil

------NID8OU5ZJYQL8H56JZ8AQE1H9S10OE--