Skip to content

Instantly share code, notes, and snippets.

@ddelpero
Created March 20, 2019 13:21
Show Gist options
  • Save ddelpero/7812e1baac86733fe61dea1c3350cd1f to your computer and use it in GitHub Desktop.
Save ddelpero/7812e1baac86733fe61dea1c3350cd1f to your computer and use it in GitHub Desktop.
Python waitress windows service
# Example class to start a Waitress server as a windows service
# the specific use case is running Waitress as a windows server using pywin32
# The Waitress docs only show how to use waitress-serve, but since waitress-serve is blocking
# you don't get a return value, which makes it impossible to gracefully stop the Waitress server
# from a windows service
# However, looking at the waitress-serve code, it's easy to write a custom class
# example pywin32 windows service: https://gist.github.com/drmalex07/10554232
from waitress.server import create_server
#import your flask app
class WaitressServer:
def __init__(self, host, port):
self.server = create_server(flask.app, host=host, port=port)
# call this method from your service to start the Waitress server
def run(self):
self.server.run()
# call this method from the services stop method
def stop_service(self):
self.server.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment