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

Commit

Permalink
Merge pull request #38 from Bart-Jellema/master
Browse files Browse the repository at this point in the history
Replace IOLoop.update_handler with remove_handler and add_handler
  • Loading branch information
FSX committed Jan 6, 2013
2 parents 95abe41 + 1893232 commit 7ad5f5c
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions momoko/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,26 +275,20 @@ 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:
# When a DatabaseError is raised it means that the connection has been
# closed and polling it would raise an exception from then IOLoop.
if not isinstance(error, psycopg2.DatabaseError):
self.ioloop.update_handler(self.fileno, 0)

if self.callback:
self.callback(error)
else:
if state == POLL_OK:
self.ioloop.update_handler(self.fileno, 0)

if self.callback:
self.callback(None)
elif state == POLL_READ:
self.ioloop.update_handler(self.fileno, IOLoop.READ)
self.ioloop.add_handler(self.fileno, self.io_callback, IOLoop.READ)
elif state == POLL_WRITE:
self.ioloop.update_handler(self.fileno, IOLoop.WRITE)
self.ioloop.add_handler(self.fileno, self.io_callback, IOLoop.WRITE)
else:
raise OperationalError('poll() returned {0}'.format(state))

Expand Down Expand Up @@ -328,7 +322,7 @@ def execute(self,
cursor = self.connection.cursor(cursor_factory=cursor_factory or base_cursor)
cursor.execute(operation, parameters)
self.callback = partial(callback or _dummy_callback, cursor)
self.ioloop.update_handler(self.fileno, IOLoop.WRITE)
self.ioloop.add_handler(self.fileno, self.io_callback, IOLoop.WRITE)

def callproc(self,
procname,
Expand Down Expand Up @@ -369,7 +363,7 @@ def callproc(self,
cursor = self.connection.cursor(cursor_factory=cursor_factory or base_cursor)
cursor.callproc(procname, parameters)
self.callback = partial(callback or _dummy_callback, cursor)
self.ioloop.update_handler(self.fileno, IOLoop.WRITE)
self.ioloop.add_handler(self.fileno, self.io_callback, IOLoop.WRITE)

def mogrify(self, operation, parameters=(), callback=None):
"""
Expand Down

0 comments on commit 7ad5f5c

Please sign in to comment.