[Twisted-Python] Asynchronicity

Simon Pickles sipickles at hotmail.com
Sat Jun 9 15:51:39 EDT 2007

Integrating into a GUI is just one example. In my case, I am using
Tkinter on win32. The GUI will be switched off when the server is
running, its simply a debug controller...

I'm more interested in how a twisted server manages to perform any other
logic. For a game server, a fair amount of time needs to be devoted to
object and memory management, AI, database access etc

My first server, in C++ could do this simply, with a winSock thread
listening and storing incoming network comms in a message queue. Easily
integrated into a game loop.

I was tempted to move to python due to the ease of modification.

So... with twisted, how can I manage to spend any time doing AI, for

I thought twisted.interface.reactor.iterate() might be my answer. API
docs say about iterate:

"Run the main loop's I/O polling function for a period of time. This is
most useful in applications where the UI is being drawn "as fast as
possible", such as games."

Unfortunately, the next sentence seems a little contradictory....

" The reactor must have been started (via the run() method) prior to any
invocations of this method. It must also be stopped manually after the
last call to this method (via the stop() method). "

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!

Am i making sense?



Jean-Paul Calderone wrote:
>On Sat, 09 Jun 2007 16:09:29 +0100, Simon Pickles <sipickles at hotmail.com> 
>>I have managed to get a simple server going. It seems to accept multiple
>>connections correctly.
>>I am a little puzzled as to how to achieve an asynchonous application
>>using twisted. Twisted itself seems asynchronous, but my code is blocked
>>by the call:
>Quite so.  reactor.run enters the Twisted mainloop and does not return
>until the mainloop is stopped (via reactor.stop).
>>Is there anyway I can return from the reactor to run other code, like
>>GUI stuff, or other processing?
>No, but you can call into your other code from Twisted event handlers or
>you can write a custom reactor which integrates an existing mainloop with
>Twisted's.  As far as GUIs go, Twisted already includes reactors which
>integrate glib, gtk, gtk2, various kinds of Windows loops, wxPython, and
>a few others.
>Precisely how you integrate two mainloops depends on various details
>about the loops in question.  Which GUI library are you trying to
>integrate with?
>Twisted-Python mailing list
>Twisted-Python at twistedmatrix.com

The next generation of Hotmail is here! http://www.newhotmail.co.uk

More information about the Twisted-Python mailing list