Commit 04105c11 by Sheng

Keep tracking worker mode, update handler when necessary

parent 08af39dc
...@@ -41,6 +41,7 @@ class Worker(object): ...@@ -41,6 +41,7 @@ class Worker(object):
self.id = str(id(self)) self.id = str(id(self))
self.data_to_dst = [] self.data_to_dst = []
self.handler = None self.handler = None
self.mode = IOLoop.READ
def __call__(self, fd, events): def __call__(self, fd, events):
if events & IOLoop.READ: if events & IOLoop.READ:
...@@ -51,9 +52,13 @@ class Worker(object): ...@@ -51,9 +52,13 @@ class Worker(object):
self.close() self.close()
def set_handler(self, handler): def set_handler(self, handler):
if self.handler: if not self.handler:
return self.handler = handler
self.handler = handler
def update_handler(self, mode):
if self.mode != mode:
self.loop.update_handler(self.fd, mode)
self.mode = mode
def on_read(self): def on_read(self):
logging.debug('worker {} on read'.format(self.id)) logging.debug('worker {} on read'.format(self.id))
...@@ -90,15 +95,15 @@ class Worker(object): ...@@ -90,15 +95,15 @@ class Worker(object):
if errno_from_exception(e) in _ERRNO_CONNRESET: if errno_from_exception(e) in _ERRNO_CONNRESET:
self.close() self.close()
else: else:
self.loop.update_handler(self.fd, IOLoop.WRITE) self.update_handler(IOLoop.WRITE)
else: else:
self.data_to_dst = [] self.data_to_dst = []
data = data[sent:] data = data[sent:]
if data: if data:
self.data_to_dst.append(data) self.data_to_dst.append(data)
self.loop.update_handler(self.fd, IOLoop.WRITE) self.update_handler(IOLoop.WRITE)
else: else:
self.loop.update_handler(self.fd, IOLoop.READ) self.update_handler(IOLoop.READ)
def close(self): def close(self):
logging.debug('Closing worker {}'.format(self.id)) logging.debug('Closing worker {}'.format(self.id))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment