Permalink
Browse files

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.
  • Loading branch information...
FSX committed Jan 6, 2013
1 parent 7ad5f5c commit 92940db0a0f6d780724f42d3d66f1b75a78430ff
Showing with 4 additions and 4 deletions.
  1. +4 −4 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()

0 comments on commit 92940db

Please sign in to comment.