Re: [Twisted-Python] Asynchronicity

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 instance? 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? Regards Simon Jean-Paul Calderone wrote:
On Sat, 09 Jun 2007 16:09:29 +0100, Simon Pickles <sipickles@hotmail.com> wrote:
Hi,
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:
reactor.run()
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?
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_________________________________________________________________ The next generation of Hotmail is here! http://www.newhotmail.co.uk

On Sat, 09 Jun 2007 20:51:39 +0100, Simon Pickles <sipickles@hotmail.com> wrote:
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...
There's Tkinter support. ;)
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
Sure (though I'm going to disregard "object and memory management" ;). If you have code for an NPC in the game, there are two possibilities for when it runs: It might run in response to events in the game world (possibly timing events). In this case, you simply implement the appropriate event handlers and it will have a chance to run at the appropriate times. It might need to run "as much as possible" - for example, to incrementally refine some behavior, to the limit imposed by available system resources. In this case, you might want to use a separate process and do IPC, or a thread might be appropriate, or you could implement something based on cooperative threading (for example, by implementing the logic as an iterator and using twisted.internet.task.Cooperator to run it as frequently as there is CPU time to do so).
[snip]
I thought twisted.interface.reactor.iterate() might be my answer. API docs say about iterate:
iterate is most likely a dead end, I wouldn't spend much time there. Jean-Paul

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
participants (3)
-
Jean-Paul Calderone
-
Maarten ter Huurne
-
Simon Pickles