Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60319
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; ';-)': 0.03; 'handler': 0.05; 'rename': 0.07; 'callback': 0.09; 'method,': 0.09; 'parameter': 0.09; 'try:': 0.09; 'useless': 0.09; 'way:': 0.09; 'def': 0.12; 'block.': 0.16; 'func': 0.16; 'handler.': 0.16; 'handlers.': 0.16; 'magic': 0.16; 'received:188.40': 0.16; 'received:188.40.28': 0.16; 'received:your-server.de': 0.16; 'reedy': 0.16; 'true:': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'library': 0.18; 'import': 0.22; 'putting': 0.22; 'header:User- Agent:1': 0.23; 'controlling': 0.24; 'helpful': 0.24; 'class.': 0.26; 'task': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'motivation': 0.31; 'received:78.46': 0.31; 'class': 0.32; 'figure': 0.32; 'interface': 0.32; 'run': 0.32; 'another': 0.32; 'comment': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'yield': 0.36; 'done': 0.36; 'method': 0.36; 'thanks': 0.36; 'similar': 0.36; 'should': 0.36; 'two': 0.37; 'implement': 0.38; 'initially': 0.38; 'version,': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; "couldn't": 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'such': 0.63; 'choose': 0.64; 'our': 0.64; 'different': 0.65; 'believe': 0.68; 'yes': 0.68; 'repeat': 0.74; 'instantly': 0.84; 'usage.': 0.84 |
| Date | Sat, 23 Nov 2013 21:30:29 +0100 |
| From | "Tobias M." <tm@tobix.eu> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Periodic execution with asyncio |
| References | <528FCCF2.4020608@tobix.eu> <l6or6b$aao$1@ger.gmane.org> <l6p0k9$utu$1@ger.gmane.org> |
| In-Reply-To | <l6p0k9$utu$1@ger.gmane.org> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Authenticated-Sender | tm@tobix.eu |
| X-Virus-Scanned | Clear (ClamAV 0.97.8/18152/Sat Nov 23 18:41:38 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.3098.1385238638.18130.python-list@python.org> (permalink) |
| Lines | 76 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1385238638 news.xs4all.nl 15973 [2001:888:2000:d::a6]:34368 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60319 |
Show key headers only | View raw
Thanks a lot for your helpful posts, Terry!
On 11/23/2013 01:00 AM, Terry Reedy wrote:
> * Make the task function a parameter 'func'.
I actually like subclassing, but yes I know there are some downsides :)
>
> * Rename start to _set to better describe what is does and call it in
> the _run function to not repeat the handler setting line. (If 'start'
> has any magic meaning, I am not aware of it. I am aware that there
> could be situations in which one would want a public method, to be
> called from other handlers. But that would be a different class ;-)
>
> * Comment out the stop method because it is useless in the current
> usage. I believe handler.cancel is only relevant after the handler is
> set and before it is fired. This can only be done from another handler.
You are right, the handler setting line should not be repeated.
I choose start() and stop() for controlling the task because this is an
interface I would instantly understand as a user of this class.
By the way: My whole motivation is, that I want to implement such a task
class in a network library I am currently writing.
On 11/23/2013 02:33 AM, Terry Reedy wrote:
> 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')
>
Thanks, now I got it. I just couldn't figure out that I have to yield
from the sleep function.
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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Periodic execution with asyncio "Tobias M." <tm@tobix.eu> - 2013-11-23 21:30 +0100
csiph-web