[Twisted-Python] Passing arguments while running a twistd application

Terry Jones terry at jon.es
Fri Oct 31 07:42:52 EDT 2008


>>>>> "Adriano" == Adriano Marques <py.adriano at gmail.com> writes:
Adriano> I have a twisted application, which formely was run with command
Adriano> line arguments which were used during runtime, eg: I was passing
Adriano> the path to the log file as argument. Then, I decided to make my
Adriano> app run through twistd as a tac file but then I'm not able to pass
Adriano> command line arguments anymore. Is there any way to overcome this
Adriano> issue?

I also ran into this, and asked for some way to pass args but got told
(paraphrasing, from memory) that that's not the way to do it...

The way to do it seems to be to convert your application to be a Twisted
plugin. It's not too hard.  Then you make sure there's a twisted/plugins
dir accessible from your path (with no __init__.py files in/under it). Then
twistd will find your plugin (just run twistd with no options and you
should see your plugin name listed at the bottom among the commands). Then
you can add options processing by subclassing the Options class in
twisted.python.usage.

Sorry to be so terse - no time. The above does work though, and seems to be
the Twisted way. Rough (untested) guideline code below.

Terry

---

from zope.interface import implements
from twisted.python import usage
from twisted.plugin import IPlugin
from twisted.application.service import IServiceMaker
from ???? import myservice

class Options(usage.Options):
    optFlags = [
        ['verbose', None, 'If True, be verbose.'],
    ]
    optParameters = [
        ['port', 'p', '0', 'The port number to listen on.'],
    ]

class MyServiceMaker(object):
    implements(IServiceMaker, IPlugin)
    # The tapname will appear in the output of twistd when run with no args.
    # Use twistd -n my-plugin-name
    # to invoke it.
    tapname = 'my-plugin-name'
    description = 'My snazzy service.'
    options = Options

    def makeService(self, options):
        return myservice(
            options['port'], options['verbose'])

serviceMaker = MyServiceMaker()




More information about the Twisted-Python mailing list