Mercurial > cosocket
changeset 85:7e3b3eb57ec2
Added the ability for a server to have a timeout function that gets called once per second.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 01 May 2009 14:18:06 -0700 |
parents | de80d8393b6b |
children | 408738c3cd5d |
files | cosocket.py |
diffstat | 1 files changed, 12 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/cosocket.py Thu Apr 30 21:01:08 2009 -0700 +++ b/cosocket.py Fri May 01 14:18:06 2009 -0700 @@ -4,6 +4,7 @@ import asynchat import types import traceback +import time class _AsyncChatCoroutineDispatcher(asynchat.async_chat): def __init__(self, coroutine, conn = None): @@ -95,8 +96,11 @@ self.continue_from_yield(data) class CoroutineSocketServer(asyncore.dispatcher): - def __init__(self, addr, coroutineFactory): + TIMEOUT = 1.0 + + def __init__(self, addr, coroutineFactory, timeout_func = None): asyncore.dispatcher.__init__(self) + self.__timeout_func = timeout_func self.__coroutineFactory = coroutineFactory self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() @@ -104,8 +108,14 @@ self.listen(1) def run(self): + start_time = time.time() while 1: - asyncore.loop(timeout = 0.5) + asyncore.loop(timeout = self.TIMEOUT, count = 1) + curr_time = time.time() + time_elapsed = curr_time - start_time + if self.__timeout_func and time_elapsed > self.TIMEOUT: + start_time = curr_time + self.__timeout_func(time_elapsed) def handle_accept(self): conn, addr = self.accept()