python - Twisted application without twistd -
python - Twisted application without twistd -
i've wrote nice app myself using twisted framework. launch using command like:
twistd -y myapp.py --pidfile=/var/run/myapp.pid --logfile=/var/run/myapp.log
it works great =)
to launch app wrote script command because i'm lazy^^ since launch app same twistd option, , tink script shell solution ugly, how can same within app? i'd launch app doing ./myapp
, without shell work around.
i've tried search in twisted documentation , reading twisted source don't understand since it's first app in python (wonderful language btw!)
thanks in advance anyhelp.
you need import twistd
script module twisted , invoke it. simplest solution this, using existing command-line, import sys
module replace argv
command line how want twistd
run, , run it.
here's simple illustration script take existing command-line , run python script instead of shell script:
#!/usr/bin/python twisted.scripts.twistd import run sys import argv argv[1:] = [ '-y', 'myapp.py', '--pidfile', '/var/run/myapp.pid', '--logfile', '/var/run/myapp.log' ] run()
if want bundle nicely bundle rather hard-coding paths, can determine path myapp.py
looking @ special __file__
variable set python in each module. adding illustration looks so:
#!/usr/bin/python twisted.scripts.twistd import run my.application import some_module os.path import join, dirname sys import argv argv[1:] = [ '-y', join(dirname(some_module.__file__), "myapp.py"), '--pidfile', '/var/run/myapp.pid', '--logfile', '/var/run/myapp.log' ] run()
and similar things compute appropriate pidfile , logfile paths.
a more comprehensive solution write a plugin twistd
. axiomatic command-line programme axiom object-database project serves tested, production-worthy illustration of how similar command-line manipulation of twistd
described above, more comprehensive handling of command-line options, different non-twistd-running utility functionality, , on.
python twisted daemon
Comments
Post a Comment