From 4d565b2305f09bcd50f88451251823d3afe24718 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Fri, 5 Dec 2008 01:58:00 +0600 Subject: [PATCH] fixed eventlet.twistedutil.block_on to handle synchronous case correctly; also, do not return anything from errback --- eventlet/twistedutil/__init__.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/eventlet/twistedutil/__init__.py b/eventlet/twistedutil/__init__.py index 43d01e2..ae6c99c 100644 --- a/eventlet/twistedutil/__init__.py +++ b/eventlet/twistedutil/__init__.py @@ -1,20 +1,30 @@ from twisted.internet import defer from twisted.python import failure from eventlet.support.greenlet import greenlet -from eventlet.api import get_hub, spawn +from eventlet.api import get_hub, spawn, getcurrent def block_on(deferred): cur = [greenlet.getcurrent()] + synchronous = [] def cb(value): if cur: - cur[0].switch(value) + if getcurrent() is cur[0]: + synchronous.append((value, None)) + else: + cur[0].switch(value) return value - def eb(err): + def eb(failure): if cur: - err.throwExceptionIntoGenerator(cur[0]) - return err - deferred.addCallback(cb) - deferred.addErrback(eb) + if getcurrent() is cur[0]: + synchronous.append((None, failure)) + else: + failure.throwExceptionIntoGenerator(cur[0]) + deferred.addCallbacks(cb, eb) + if synchronous: + result, failure = synchronous[0] + if failure is not None: + failure.raiseException() + return result try: return get_hub().switch() finally: