[Twisted-Python] Twisted and python-ogre

Jean-Paul Calderone exarkun at divmod.com
Thu Feb 14 11:01:58 EST 2008


On Thu, 14 Feb 2008 16:32:30 +0100, Thomas Boucher <thomas.boucher at student.ecp.fr> wrote:
>Hello,
>
>I am currently part of a MMORPG project in python, using Twisted
>and python-ogre. I found some posts talking about the cohabitation between 
>the two. However I couldn't find a definitive answer. What I am seeking for 
>is more a general method than technical details.
>
>What I would like to do is to put properly Twisted and python-ogre
>together. To do that I have already tried the following ways :
>
>  - First run python-ogre in the main loop. At a time, this loop launches
>the twisted reactor in another thread with Thread(target =
>sthg_which_runs_reactor.run).
>The problem I am experiencing is described here [1]. The sendLine method
>is executed immediately, but the data is received by the other side only
>20/30 seconds after.

Chris Armstrong's response to the post you refer to is accurate.  Twisted
APIs are not threadsafe.  In order to call them from a thread other than
the one in which the reactor is running, it is absolutely required that you
use reactor.callFromThread.  This is quite simple.  Instead of:

    proto.sendLine("line")

Simple do:

    reactor.callFromThread(proto.sendLine, "line")

You will have to do this for every call you make to a Twisted API (except
for reactor.callFromThread, of course ;).

Since this is quite inconvenient, inefficient, and error prone, it's best
if you keep the interaction between different threads to the barest minumum
you can manage.

>
>  - So I tried not to run Twisted in another thread, by using the way
>described here [2]. Twisted is given the main loop and the
>rendering is done frame by frame with the coiterate. It works sometimes,
>the renderOneFrame in the coiterration is not really working.
>

What do you mean "not really working"?

>
>But it seems more logical to run ptyhon-ogre in the main loop and
>twisted on the side (in another thread for example, as soon as the client 
>has decided to connect to the server). Have you any ideas how I could do 
>this properly (with threads, without threads, with the coiterrate ...)?

The best solution would be to really integrate the two loops.  I don't know
the details of OGRE though, so I can't tell you specifically how to do this.
It is tightly dependent on the details of the loops being integrated.

>
>If Twisted was originally made for MMORPGs, why the reactor wants so much to 
>be in the main loop and to manage itself the other threads (with 
>callInThread for example)?
>

Because most other event loops are very bad at managing events. :)  If you
want reasonable network performance, you need a good network event loop to
be in control.

Jean-Paul




More information about the Twisted-Python mailing list