Re: [Twisted-Python] Asynchronicity
Just to point out, iterate() does seem to run a loop of reactor, before returning. No call to run() is required. This is contrary to the API docs. Why would iterate() be a dead end, jean? Simon Pickles wrote:
Wow! it works... iterate does indeed return. Must've had a small mistake before!
I am amazed I solved my own problem for a change!
Thanks for the advice, Jean
Simon Pickles 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...
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
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_________________________________________________________________ Play your part in making history - Email Britain! http://www.emailbritain.co.uk/
_______________________________________________ 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 Sun, 10 Jun 2007 10:43:48 +0100, Simon Pickles <sipickles@hotmail.com> wrote:
Just to point out, iterate() does seem to run a loop of reactor, before returning. No call to run() is required.
This is contrary to the API docs.
The API docs describe the supported behavior. Implementation details may allow other behavior to appear to work accidentally.
Why would iterate() be a dead end, jean?
Generally speaking, none of the reactors are reentrant and driving them stepwise with iterate() is not a feature which is seeing any interest by maintainers these days. This means that it may or may not work with each reactor and there is little test coverage for it. Jean-Paul
participants (2)
-
Jean-Paul Calderone
-
Simon Pickles