CherryPy Recipes / deployment / waitress_ /

# -*- coding: utf-8 -*-
__doc__ = """ Demonstrates on how to run
a CherryPy application within a waitress WSGI server.

It requires waitress

This recipe provides two ways of integrating the waitress
server with the CherryPy engine's loop. Either the engine is
responsible for asking the waitress server to loop once or
the waitress server is run into its own thread and, therefore,
not dependant from the rate of the engine's loop.

The second solution is probably less likely to cause unwanted
delay for request processing since the server is able to
run at its own pace. However this means running the
server in a thread which may not be suitable for everyone.
import cherrypy

class Root(object):
    def index(self):
        return "Hello world waitress"

if __name__=="__main__":
    # Logging is performed by waitress
        'log.screen': True
    # Mount the application so that it
    # served by the CherryPy application server
    # The returned app is a WSGI application
    app = cherrypy.tree.mount(Root())

    # Siince we are using the waitress WSGI server
    # we prevent the CherryPy HTTP server
    # from being started

    # Bus plugin that will run the waitress server
    # whilst behaving properly with the bus itself
    # Runs the server in the bus mainloop using the
    # bus's loop internal rate.
    # from waitressplugin import WaitressServerPlugin
    # WaitressServerPlugin(app, host='', port=8090).subscribe()   

    # Bus plugin that will run the waitress server
    # inside in its own thread independantly from
    # the bus mainloop
    from waitressplugin import ThreadedWaitressServerPlugin
    ThreadedWaitressServerPlugin(app, host='', port=8090).subscribe()
    # Finally we run the CherryPy application server