[Twisted-Python] Twistd and application framework questions

Rob Hoadley hoadley at gmail.com
Wed Apr 1 19:42:02 EDT 2009


I can help you with this question:

> 2)  There are still scenarios where I would want to run my code the
> "reactor" way rather than than using the application framework, and I would
> love to be able to keep a single file that's compatible with both modes..
> Is there a way to detect in runtime whether the code is being run through
> 'twistd' and the app framework or if its run directly?
> I was thinking of doing something like:
>

I'm sure there's a way to introspect and figure out how you are being
run. But...

Why do you want to do this?

If it's because you have different environments between prod and
development then you can look at twisted plugins or I just use ENV
vars from my os.environ.  When the app starts it pulls os.environ
vars.

If it's to debug? You can twistd -noy.

I'd also recommend that you are better off in the long run if you
create a number of unittests. trial offers a way to run unittests
within the reactor.  If you haven't done this step it's a major
lifesaver. Tests help you understand how your service,protocol,etc
react and gives you confidence to roll all your services code together
and have it just work.  I've even used trial to do all kinds of crazy
stuff  in my normal non-twisted python coding. Also buildbot to
automatically run the tests is handy.

If it's to run just one service? You'd add this to the bottom of your
service file (i'm assuming you've separated your service from your tac
file)

if __name__ == "__main__":
    #start your service/services here

the tac file won't execute main when importing the service.  So it's
not two files.  But I'm not sure what your end goal is here.  Do tell.

-rob




More information about the Twisted-Python mailing list