[Twisted-Python] blocking code in object constructors

Tommi Virtanen tv at twistedmatrix.com
Fri Jun 27 08:17:21 EDT 2003

On Fri, Jun 27, 2003 at 01:40:32PM +0400, Egor Cheshkov wrote:
> I need to do some blocking stuff in object constructor. However constructors 
> can't return deferreds. The solution I see is to put all blocking 
> initialization to some method and use factory to generate objects. Factory 
> will return deferred wich will be called back when object initialization is 
> complete. Is there Twisted Standard way to do such things?

	Not that I know of, but it's dead simple.

	Of course, first you have to replace all that blocking stuff.
	Deferred doesn't make it ok for you to block; it makes it ok
	for async things to complete later.

class ThingieThatUsedToBlock:
    def __init__(self, foo, bar):
	# don't block or even do the async thing here

    def setUp(self):
	# don't block here, but do start the async init.
	deferred = startTheAsyncThingie()
	deferred.addCallback(lambda _: self)
	return deferred

def createThingie(*a, **kw):
    t = ThingieThatUsedToBlock(*a, **kw)
    d = t.setUp()
    return d

:(){ :|:&};:

More information about the Twisted-Python mailing list