Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Commit

Permalink
Don't remove IOLoop handler of a connection on every IO callback.
Browse files Browse the repository at this point in the history
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 92940db
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions momoko/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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.