Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60252
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!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 | <tm@tobix.eu> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.023 |
| X-Spam-Evidence | '*H*': 0.95; '*S*': 0.00; '(using': 0.07; 'callback': 0.09; 'constructor': 0.09; 'pep': 0.09; 'solution,': 0.09; 'python': 0.11; 'def': 0.12; 'above?': 0.16; 'guys,': 0.16; 'itself,': 0.16; 'received:188.40': 0.16; 'received:188.40.28': 0.16; 'received:your-server.de': 0.16; 'tasks,': 0.16; 'import': 0.22; 'header:User-Agent:1': 0.23; 'questions:': 0.24; 'task': 0.26; 'second': 0.26; 'tried': 0.27; 'function': 0.29; 'specified': 0.30; 'code': 0.31; 'received:78.46': 0.31; 'class': 0.32; 'interface': 0.32; 'run': 0.32; '(e.g.': 0.33; 'call.': 0.33; 'implemented': 0.33; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'there': 0.35; 'method': 0.36; 'seconds': 0.37; 'too': 0.37; 'two': 0.37; 'implement': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'how': 0.40; 'easy': 0.60; 'skip:a 30': 0.61; 'first': 0.61; 'such': 0.63; 'provide': 0.64; 'periodically': 0.68; 'containing': 0.69; 'asynchronous': 0.84; 'self.run()': 0.84; 'do:': 0.91 |
| Date | Fri, 22 Nov 2013 22:30:26 +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 | Periodic execution with asyncio |
| 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/18148/Fri Nov 22 18:41:03 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.3057.1385157109.18130.python-list@python.org> (permalink) |
| Lines | 61 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1385157109 news.xs4all.nl 15888 [2001:888:2000:d::a6]:51352 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:60252 |
Show key headers only | View raw
Hello guys,
I am using the asyncio package (Codename 'Tulip'), which will be
available in Python 3.4, for the first time.
I want the event loop to run a function periodically (e.g. every 2
seconds). PEP 3156 suggests two ways to implement such a periodic call:
1. Using a callback that reschedules itself, using call_later().
2. Using a coroutine containing a loop and a sleep() call.
I implemented the first approach in a class with an easy to use
interface. It can be subclassed and the run() method
can be overwritten to provide the code that will be called periodically.
The interval is specified in the constructor and the task can be started
and stopped:
import asyncio
class PeriodicTask(object):
def __init__(self, interval):
self._interval = interval
self._loop = asyncio.get_event_loop()
def _run(self):
self.run()
self._handler = self._loop.call_later(self._interval, self._run)
def run(self):
print('Hello World')
def start(self):
self._handler = self._loop.call_later(self._interval, self._run)
def stop(self):
self._handler.cancel()
To run this task and execute run() every 2 seconds you can do:
task = PeriodicTask(2)
task.start()
asyncio.get_event_loop().run_forever()
So far, this works for me. But as I have no experience with asynchronous
IO I have two questions:
1. Is this a reasonable implementation or is there anything to improve?
2. How would you implement the second approach from the PEP (using a
coroutine) with the same interface as my PeriodicTask above?
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.
Regards,
Tobias
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Periodic execution with asyncio "Tobias M." <tm@tobix.eu> - 2013-11-22 22:30 +0100
csiph-web