Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60538
| 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 | <tm@tobix.eu> |
| 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> <l6or6b$aao$1@ger.gmane.org> <l6p0k9$utu$1@ger.gmane.org> <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." <tm@tobix.eu> |
| 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 <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.3254.1385495552.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[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