[Twisted-Python] Asynchronicity

Maarten ter Huurne maarten at treewalker.org
Sat Jun 9 16:18:43 EDT 2007


On Saturday 09 June 2007, Simon Pickles wrote:

> Now, I am only a beginner at twisted, but it looks to me if I called
> run(), I won't have a chance to call iterate() cos twisted will have
> gone into its mainloop and stolen all control!

You should make your code run inside of (under control of) the reactor. I 
think what you're looking for is the callLater method:

reactor.callLater(delay_in_seconds, function, arg1, arg2, ...)

In the function, do a little bit of work, then schedule another callLater and 
let the function return. That way, the reactor can handle the network traffic 
before going back to processing for example AI.

If it's not easy to divide the AI code in fast-executing chunks, you could 
consider running it on a separate thread, but then you have to be careful to 
properly guard all concurrent access to shared data. Another option is to run 
in the AI in a different process, if there is not too much communication 
between the network part and the AI.

Bye,
		Maarten
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20070609/200293e3/attachment.pgp 


More information about the Twisted-Python mailing list