[issue1641] asyncore delayed calls feature
Giampaolo Rodola'
report at bugs.python.org
Wed Dec 14 15:24:38 CET 2011
Giampaolo Rodola' <g.rodola at gmail.com> added the comment:
Now that I think of it maybe some kind of wrapper would still be necessary.
As of right now, we'd do something like this.
At the core we would have:
import asyncore, asynchat, sched
# global
scheduler = sched.scheduler()
while 1:
asyncore.loop(timeout=1.0, count=1) # count=1 makes loop() return after 1 loop
scheduler.run(blocking=False)
Then, every dispatcher can define a scheduled function of its own:
class Client(asynchat.async_chat):
# an already connected client
# (the "connector" code is not included in this example)
def __init__(self, *args, **kwargs):
asynchat.async_chat.__init__(self, *args, **kwargs)
self.set_terminator("\r\n")
self.set_timeout()
def set_timeout(self):
self.timeout = scheduler.enter(30, 0, self.handle_timeout)
def reset_timeout(self):
scheduler.cancel(self.timeout)
self.set_timeout()
def found_terminator(self):
scheduler.cancel(self.timeout)
self.timeout = scheduler.enter(30, 0, self.handle_timeout)
# do something with the received data...
def handle_timeout(self):
self.push("400 connection timed out\r\n")
self.close()
def close(self):
scheduler.cancel(self.timeout)
asynchat.async_chat.close(self)
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1641>
_______________________________________
More information about the Python-bugs-list
mailing list