Re: [Twisted-Python] Passing arguments while running a twistd application
![](https://secure.gravatar.com/avatar/c8f4d4d1a8fc53c13ed05c202e0257fe.jpg?s=120&d=mm&r=g)
"Adriano" == Adriano Marques <py.adriano@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()
![](https://secure.gravatar.com/avatar/7ed9784cbb1ba1ef75454034b3a8e6a1.jpg?s=120&d=mm&r=g)
On Fri, 31 Oct 2008 12:42:52 +0100, Terry Jones <terry@jon.es> wrote:
"Adriano" == Adriano Marques <py.adriano@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...
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 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()
Fortunately, in case the above misses anything or you'd like any parts of it explained, there is documentation about writing plugins. :) http://twistedmatrix.com/projects/core/documentation/howto/plugin.html http://twistedmatrix.com/projects/core/documentation/howto/tap.html Jean-Paul
![](https://secure.gravatar.com/avatar/68599b3b210ee81b094256921722ecc6.jpg?s=120&d=mm&r=g)
Ok guys, I'll be porting my app to plugin instead. Thanks for the answers. Kind Regards, -- Adriano Monteiro Marques http://adriano-marques.blogspot.com http://www.umitproject.org http://www.pythonbenelux.org "Don't stay in bed, unless you can make money in bed." - George Burns
participants (3)
-
Adriano Marques
-
Jean-Paul Calderone
-
Terry Jones