Two WSGI servers with access to the same application instance?

116 views
Skip to first unread message

Nick Repole

unread,
Apr 30, 2013, 11:17:29 AM4/30/13
to cherryp...@googlegroups.com
Hello all,

I'm looking to run two WSGI servers (one HTTP, one HTTPS) with access to the same instance of my application. As far as I can tell, the only way to do this is to split off two threads in my server file:

from myapp import app
from cherrypy import wsgiserver
from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter
import threading

dispatcher = wsgiserver.WSGIPathInfoDispatcher({'/': app})
http_server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8080), dispatcher)
https_server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8081), dispatcher)
https_server.ssl_adapter = BuiltinSSLAdapter('keys\server.crt', 'keys\server.crtkey')


if __name__ == '__main__':
    http_thread = threading.Thread(target=http_server.start)
    https_thread = threading.Thread(target=https_server.start)
    try:
        http_thread.start()
        https_thread.start()
    except KeyboardInterrupt:
        http_thread.join()
        https_thread.join()
        http_server.stop()
        https_server.stop()


Is this bad design? Should I be doing something differently?

Sylvain Hellegouarch

unread,
Apr 30, 2013, 2:54:21 PM4/30/13
to cherryp...@googlegroups.com
Hi,
In my opinion you should use the CherryPy engine layer if you could. It'd make your life easier. If not, have you look at this?

Nick Repole

unread,
May 1, 2013, 3:58:21 PM5/1/13
to cherryp...@googlegroups.com
I do currently have my app running that way, was more just curious from an experimentation point of view. On a similar note, was curious if I'd be able to run a single instance of a cherrypy wsgi app behind something like nginx with both http and https.

Derek Litz

unread,
May 2, 2013, 11:43:12 AM5/2/13
to cherryp...@googlegroups.com
 Through cherrypy:

    cherrypy.tree.mount(Root(), config=conf)
    cherrypy.server.thread_pool = 30
    cherrypy.server.ssl_private_key = 'something.key'
    cherrypy.server.ssl_certificate = 'something.crt'
    cherrypy.server.socket_port = 443
    cherrypy.server.socket_host = '0.0.0.0'

    http_server = cherrypy._cpserver.Server()
    http_server.thread_pool = 30
    http_server.socket_port = 80
    http_server.socket_host = '0.0.0.0'
    http_server.subscribe()

    cherrypy.engine.start()
    cherrypy.engine.block()

WSGI Interface, SSL is handled by application server container (Apache or nginx for example):

    application = cherrypy.Application(Root(), '/', config=conf)

Nick Repole

unread,
May 4, 2013, 5:37:24 PM5/4/13
to cherryp...@googlegroups.com
Thanks Derek, the first part is what I currently have, second part is more what I was looking for.

My understanding is I still need to call cherrypy.engine.start() after creating my app object? 

Also curious, what's the difference between:
app = cherrypy.tree.mount(MyApp(), "", 'config.ini')
app = cherrypy.Application(MyApp(), "", 'config.ini')
Reply all
Reply to author
Forward
0 new messages