[Twisted-Python] Blocking

Moshe Zadka moshez at zadka.com
Wed Sep 26 06:29:12 EDT 2001

I whipped up the following to help regular client applications use
pb even if they are written in a linear manner.

class _Blocker:

    callbackName = 'callback'
    errbackName = 'errback'

    def __init__(self, method, args, kw):
        self.method = method
	self.args = args
	self.kw = kw

    def callback(self, value):
        self.state = 1
        self.value = value

    def errback(self, value=None):
        self.state = 0
	self.value = value

    def block(self):
        kw = self.kw.copy()
	kw[self.callbackName] = self.callback
	kw[self.errbackName] = self.errback
        apply(self.method, args, kw)
	if self.state:
	    return self.value
        raise BlockingError, self.value

def block(method, *args, **kw):
    return _Blocker(method, args, kw).block()

Any feedback? 

More information about the Twisted-Python mailing list