[Twisted-Python] Twistd .tac + wxpython

John Crawford cyclops at speakeasy.net
Thu Aug 20 13:55:20 EDT 2009


>I have some services written that I'd like to run in a ta.c w/ twistd.
>One of them uses wxpython. I originally used the wxreactor but it seams
>to have some problems on Mac OS X and Windows, so with help from some
>people on this list I ended up using the wx mainloop as the mainloop and
>putting the reactor in a separate thread. This seams to work well, but
>now that I want to create a .tac I don't think this is possible.

I have been working through the Finger example section, and also use 
wxPython for a GUI, and although it was a bit tricky to setup, it 
works fine. I also setup my Finger server code as a .tac, and twistd 
starts it up also. Here is some of the code fragments I used:

     # finger_server.py
     from twisted.python import log
     from twisted.internet import wxreactor

     wxreactor.install()

     # import t.i.reactor only after installing wxreactor:
     from twisted.application import internet, service
     from twisted.internet import reactor, protocol, defer
     from twisted.protocols import basic

The first step is importing *and* installing wxreactor. After that, 
you have to create and register a wxPython App with the Reactor, and 
it takes care of the wx loop.

     app = MyApp(0)
     reactor.registerWxApp(app)
     msgWin = app.frame.foundText # this is a textCtrl I send output to.

     fs = FingerService(msgWin)
     ff = fs.getFingerFactory()
     fsf = fs.getFingerSetterFactory()
     global application
     application = service.Application('finger', uid=1, gid=1)
     sc = service.IServiceCollection(application)
     internet.TCPServer(79, ff).setServiceParent(sc)
     internet.TCPServer(179, fsf).setServiceParent(sc)

In my code, I actually use:

         if __name__ == '__main__':
             main_code()
         else:
             service_code()

This lets me run the code in Eclipse for testing, then I copy the .py 
file to a .tac file, and it works as a twistd daemon, including 
popping up a wxPython window for output. It *also* works as a 
standalone file when I use py2exe on the .py file - but I don't see 
how you could use twistd with a .exe, since twistd expects a script.

I hope this is clear - if not, I can send you my entire 
finger_server.py file, for your amusement :)

John C>




More information about the Twisted-Python mailing list