[Twisted-Python] How to exit a twistd program with a status code ?

Paul Goins general at vultaire.net
Fri Sep 24 09:39:23 EDT 2010

I'd like to add my two cents here; maybe this may be valuable to someone.

I might suggest, if reasonable, to allow for running the app with or 
without twistd.  twistd is really handy for 
profiling/debugging/daemonizing/etc., but it does lack in other areas 
such as command line args and (new to me) return codes.

The last few apps I've done, I've set up a standard Python entry point like:

   #!/usr/bin/env python
   from twisted.internet import reactor
   from myapp import main

   if __name__ == "__main__":
       # For quick/easy logging when running undaemonized


Along with something for creating the application object in .tac files:

   from twisted.application.service import Application
   from twisted.internet import reactor
   from twisted.python.syslog import SyslogObserver
   from myapp import main

   def create_application():
       app = Application("foo")

       # More robust logging for daemonized (production) mode
       observer = SyslogObserver("bar")
       app.setComponent(ILogObserver, observer.emit)

       return app

And then, you end up with possibly a really simple .tac file:

   import myapp.tacapp
   application = myapp.tacapp.create_application()

That seems to give the best of both worlds from what I've seen.  Haven't 
seen any key drawbacks yet.

- Paul Goins

More information about the Twisted-Python mailing list