From 92940db0a0f6d780724f42d3d66f1b75a78430ff Mon Sep 17 00:00:00 2001 From: FSX Date: Sun, 6 Jan 2013 19:25:43 +0100 Subject: [PATCH] Don't remove IOLoop handler of a connection on every IO callback. Instead, add handler at the start of an operation (e.g. opening a connection, or executing an SQL query), update the handler everything an event occurs (reading and writing) and remove the handler when the operation is done or when an error occurs. --- momoko/connection.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/momoko/connection.py b/momoko/connection.py index 80ebe28..8195f5c 100644 --- a/momoko/connection.py +++ b/momoko/connection.py @@ -275,20 +275,21 @@ def __init__(self, self.ioloop.add_handler(self.fileno, self.io_callback, IOLoop.WRITE) def io_callback(self, fd=None, events=None): - self.ioloop.remove_handler(self.fileno) try: state = self.connection.poll() except (psycopg2.Warning, psycopg2.Error) as error: + self.ioloop.remove_handler(self.fileno) if self.callback: self.callback(error) else: if state == POLL_OK: + self.ioloop.remove_handler(self.fileno) if self.callback: self.callback(None) elif state == POLL_READ: - self.ioloop.add_handler(self.fileno, self.io_callback, IOLoop.READ) + self.ioloop.update_handler(self.fileno, IOLoop.READ) elif state == POLL_WRITE: - self.ioloop.add_handler(self.fileno, self.io_callback, IOLoop.WRITE) + self.ioloop.update_handler(self.fileno, IOLoop.WRITE) else: raise OperationalError('poll() returned {0}'.format(state)) @@ -476,5 +477,4 @@ def close(self): """ Remove the connection from the IO loop and close it. """ - self.ioloop.remove_handler(self.fileno) self.connection.close()