Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60538
| References | <528FCCF2.4020608@tobix.eu> <l6or6b$aao$1@ger.gmane.org> <l6p0k9$utu$1@ger.gmane.org> <52911065.6080308@tobix.eu> <20131125105254.GA9462@phconnel-ws.cisco.com> |
|---|---|
| Subject | Re: Periodic execution with asyncio |
| From | "Tobias M." <tm@tobix.eu> |
| Date | 2013-11-26 17:35 +0100 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3254.1385495552.18130.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
Thanks Phil, now I understand! I wasn't aware of the fact that tasks are automatically attached to the event loop when they are created via their constructor. I thought I have to pass them to a run_* method explicitly. Phil Connell <pconnell@gmail.com> schrieb: >On Sat, Nov 23, 2013 at 09:30:29PM +0100, Tobias M. wrote: >> Now putting this into a PeriodicTask class that provides a similar >interface >> like our callback version, I get: >> >> >> import asyncio >> >> class PeriodicTask2(object): >> >> def __init__(self, func, interval): >> self.func = func >> self.interval = interval >> self._loop = asyncio.get_event_loop() >> >> def start(self): >> self.loop.run_until_complete(asyncio.Task(self._run())) >> >> @asyncio.coroutine >> def _run(self): >> while True: >> yield from asyncio.sleep(self.interval) >> self.func() >> >> >> I don't know if I misunderstood anything, but as a user of this class >I am >> not able to run two task simultaneously because start() will block. >In the >> callback version I could instanciate two PeriodicTasks and run them >both at >> the same time (after a call to loop.run_forever()), which is actually >what I >> wanted to achieve with this class. > >You need to separate out creating tasks from running the event loop. > >Specifically, you should run the event loop in exactly one place, >probably at >the top level of your script. > > >A toy example: > >import asyncio > >@asyncio.coroutine >def adder(*args, delay): > yield from asyncio.sleep(delay) > print(sum(args)) > >def main(): > asyncio.Task(adder(1, 2, 3, delay=5)) > asyncio.Task(adder(10, 20, delay=3)) > > loop = asyncio.get_event_loop() > loop.run_forever() > >if __name__ == "__main__": > main() > > >$ ./python test.py >30 >6 > > >Note that run_forever() will block indefinitely (as the name suggests >:). This >is generally what you'll want for a long-running server. > >If you want do something more like: > - Spawn some tasks > - Run the event loop until they're done > - Exit > >then you'll need to use loop.run_until_complete(asyncio.gather(*tasks)) >instead. > > >HTH, >Phil
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Periodic execution with asyncio "Tobias M." <tm@tobix.eu> - 2013-11-26 17:35 +0100
csiph-web