def blockingCallFromGreenlet(reactor,func, *args,**kwargs): """Call function in reactor greenlet and block current greenlet waiting for the result""" result = AsyncResult() def task(): try: result.set(func(*args,**kwargs)) except Exception as ex: result.set_exception(ex) reactor.callFromGreenlet(task) value = result.get() if isinstance(value,defer.Deferred): return waitForDeferred(value) else: return value
try: return result.get() except failure.Failure,ex: ex.raiseException() def blockingCallFromGreenlet(*args,**kwargs): """Call function in reactor greenlet and block current greenlet waiting for the result""" reactor = args[0] func = args[1] result = AsyncResult() def task(): try: result.set(func(*args[2:],**kwargs)) except Exception,ex: result.set_exception(ex) reactor.callFromGreenlet(task) value = result.get() if isinstance(value,defer.Deferred): return waitForDeferred(value) else: return value class IReactorGreenlets(Interface): """Interface for reactor supporting greenlets""" def getGreenletPool(self): pass def callInGreenlet(self,*args,**kwargs): pass def callFromGreenlet(self,*args,**kw): pass def suggestGreenletPoolSize(self,size):
def post_stop(self): try: reactor.callFromGreenlet(reactor.stop) except ReactorNotRunning: pass
def blockingCallFromGreenlet(*args, **kwargs): """Call function in reactor greenlet and block current greenlet waiting for the result""" reactor = args[0] assert reactor.greenlet != getcurrent(), "can't invoke this in the reactor greenlet" func = args[1] result = AsyncResult() def task(): try: result.set(func(*args[2:], **kwargs)) except Exception, ex: result.set_exception(ex) reactor.callFromGreenlet(task) value = result.get() if isinstance(value, defer.Deferred): return waitForDeferred(value) else: return value class IReactorGreenlets(Interface): """Interface for reactor supporting greenlets""" def getGreenletPool(self): pass def callInGreenlet(self, *args, **kwargs): pass