[Twisted-Python] Passing arguments while running a twistd application
exarkun at divmod.com
Fri Oct 31 09:12:19 EDT 2008
On Fri, 31 Oct 2008 12:42:52 +0100, Terry Jones <terry at jon.es> wrote:
>>>>>> "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
>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...
To add slightly to this correct answer:
The "tac" in a .tac file stands for "Twisted Application Configuration".
The reason you cannot pass extra arguments on the command line when using
a .tac file is that the intent is for all configuration to be in the .tac
file. So if this isn't appropriate for your application, a twistd plugin
is probably a better solution.
>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
>Sorry to be so terse - no time. The above does work though, and seems to be
>the Twisted way. Rough (untested) guideline code below.
>from zope.interface import implements
>from twisted.python import usage
>from twisted.plugin import IPlugin
>from twisted.application.service import IServiceMaker
>from ???? import myservice
> optFlags = [
> ['verbose', None, 'If True, be verbose.'],
> optParameters = [
> ['port', 'p', '0', 'The port number to listen on.'],
> 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()
Fortunately, in case the above misses anything or you'd like any parts
of it explained, there is documentation about writing plugins. :)
More information about the Twisted-Python