[Twisted-Python] Blocking

Moshe Zadka moshez at zadka.com
Wed Sep 26 04:29:12 MDT 2001


Hi!
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
	main.shutDown()

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

    def block(self):
        kw = self.kw.copy()
	kw[self.callbackName] = self.callback
	kw[self.errbackName] = self.errback
        apply(self.method, args, kw)
	main.run()
	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