示例#1
0
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
示例#2
0
	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):
示例#3
0
文件: runner.py 项目: eallik/spinoff
 def post_stop(self):
     try:
         reactor.callFromGreenlet(reactor.stop)
     except ReactorNotRunning:
         pass
示例#4
0
 def post_stop(self):
     try:
         reactor.callFromGreenlet(reactor.stop)
     except ReactorNotRunning:
         pass
示例#5
0

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