Last active
August 12, 2016 23:57
-
-
Save DeaconDesperado/6295116 to your computer and use it in GitHub Desktop.
Run cherrypy WSGI server as a daemon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cherrypy.process.plugins import Daemonizer,PIDFile | |
import cherrypy | |
import argparse | |
parser = argparse.ArgumentParser(description="My server daemon") | |
parser.add_argument('-d','--daemon',help='Run the server as a daemon using the traditional double fork',action='store_true') | |
parser.add_argument('-a','--bind-address',help='Network interface to bind to',default='127.0.0.1') | |
parser.add_argument('-p','--port',help='Port to bind to',default=8080,type=int) | |
parser.add_argument('--pidfile',help='process id file',type=str) | |
args = parser.parse_args() | |
#this is your wsgi app: it could be a flask, werkzeug, pylons app etc | |
def application(environ, start_response): | |
start_response('200 OK', [('content-type', 'text/plain')]) | |
return ('Hello world!',) | |
cherrypy.config.update({ | |
'server.socket_host':args.bind_address, | |
'server.socket_port':args.port | |
}) | |
cherrypy.tree.graft(application,'/') | |
if args.daemon: | |
Daemonizer(cherrypy.engine).subscribe() | |
if args.pidfile: | |
PIDFile(cherrypy.engine,args.pidfile).subscribe() | |
cherrypy.engine.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why do you not use the 'cherryd -d' command for daemonize?