[Twisted-Python] at what point does reactor.run() need to be called?

Michal Pasternak michal at pasternak.w.lub.pl
Tue Apr 6 04:01:27 MDT 2004


Damon Fasching [Mon, Apr 05, 2004 at 11:39:57PM -0700]:
> The following two modules work fine together.

reactor.run() enters an event-controlled loop (it's the same as "mainloop"
in GUI toolkits you could program in), so you should call it after you do
all initialization stuff.

> However, if the call to reactor.run() is moved any
> higher in client.py, the client seems to hang.  For

It doesn't hang, it just gets the root object, but has no callback to pass
it to.

If it hangs (locks-up hard), it's a bug, examine & report it.

> I would like to be able to dynamically connect to new
> servers after starting my application and intereacting
> with other servers, and so after already having called
> reactor.run().  Can I do this?  I'm sure it is
> possible.  But the quick test above makes it seem that
> I have to invoke getRootObject() AND attach a callback
> to the returned deferred before I can invoke
> reactor.run().

Yes, that's right.

The point you've missed:

	def connectToNewServersNStuff():
		# connect to one server
		pass
	
	# do all initialisation
	# ...
	
	reactor.callLater(0, connectToNewServersNStuff)	
	reactor.run()	

The value, that getRootObject() returns is a Deferred object. This is a
"delayed" function call (which is also non-blocking), so the *real*
rootObject is passed to the Deferred.callback function.

-- 
mp




More information about the Twisted-Python mailing list