[Twisted-Python] blocking code in object constructors
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.
def __init__(self, foo, bar):
# don't block or even do the async thing here
# don't block here, but do start the async init.
deferred = startTheAsyncThingie()
deferred.addCallback(lambda _: self)
def createThingie(*a, **kw):
t = ThingieThatUsedToBlock(*a, **kw)
d = t.setUp()
More information about the Twisted-Python