Skip to content

Commit

Permalink
IOLoop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wil committed Jan 7, 2010
1 parent 614e5e0 commit 578f50d
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/gt/monkey.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,34 +9,44 @@ def patch_tornado_ioloop():
_IOLoop = _tornado_iol.IOLoop _IOLoop = _tornado_iol.IOLoop


class IOLoop: class IOLoop:
READ = _IOLoop.READ
WRITE = _IOLoop.WRITE
ERROR = _IOLoop.ERROR

def __init__(self): def __init__(self):
self._handlers = {} # by fd self._handlers = {} # by fd
self._events = {} # by fd self._events = {} # by fd


def start(self): def start(self):
gevent.hub.get_hub().switch() gevent.hub.get_hub().switch()


def stop(self):
for e,fd in list(self._events.iteritems()):
self.remove_handler(e)

gevent.hub.shutdown()

def remove_handler(self, fd): def remove_handler(self, fd):
self._handlers.pop(fd, None) self._handlers.pop(fd, None)
ev = self._events.pop(fd, None) ev = self._events.pop(fd, None)
ev.cancel() ev.cancel()


def update_handler(self, fd, events): def update_handler(self, fd, events):
handler = self._handlers.pop(fd, None) handler = self._handlers.pop(fd, None)
ev = self._events[fd]
self.remove_handler(fd) self.remove_handler(fd)
self.add_handler(fd, handler, events) self.add_handler(fd, handler, events)


def add_handler(self, fd, handler, events): def add_handler(self, fd, handler, events):
type = 0 type = gevent.core.EV_PERSIST
if events & _IOLoop.READ: if events & _IOLoop.READ:
type = type | gevent.core.EV_READ type = type | gevent.core.EV_READ
if events & _IOLoop.WRITE: if events & _IOLoop.WRITE:
type = type | gevent.core.EV_WRITE type = type | gevent.core.EV_WRITE
if events & _IOLoop.ERROR: if events & _IOLoop.ERROR:
type = type | gevent.core.EV_SIGNAL type = type | gevent.core.EV_READ


def callback(ev, type): def callback(ev, type):
#print "ev=%r type=%r firing" % (ev, type)
tornado_events = 0 tornado_events = 0
if type & gevent.core.EV_READ: if type & gevent.core.EV_READ:
tornado_events |= _IOLoop.READ tornado_events |= _IOLoop.READ
Expand All @@ -46,7 +56,11 @@ def callback(ev, type):
tornado_events |= _IOLoop.ERROR tornado_events |= _IOLoop.ERROR
return handler(ev.fd, tornado_events) return handler(ev.fd, tornado_events)


self._events[fd] = gevent.core.event(type, fd, callback) #print "add_handler(fd=%r, handler=%r, events=%r)" % (fd, handler, events)
#print "type => %r" % type
e = gevent.core.event(type, fd, callback)
e.add()
self._events[fd] = e
self._handlers[fd] = handler self._handlers[fd] = handler




Expand All @@ -60,8 +74,8 @@ def add_timeout(self, deadline, callback):


@classmethod @classmethod
def instance(cls): def instance(cls):
print "new instance?"
if not hasattr(cls, "_instance"): if not hasattr(cls, "_instance"):
print "new instance?"
cls._instance = cls() cls._instance = cls()
return cls._instance return cls._instance


Expand Down

0 comments on commit 578f50d

Please sign in to comment.