I have seen that gunicorn provide server hooks that we can use to hook on the various server event , I am looking for the same in hypercorn as it is inspired on gunicorn , but the hypercorn documentation is not of any help in this matter and i haven't found anyone implementing that .
hyperconr.config.py
import multiprocessing as mp
import os
print( (mp.cpu_count() * 2) + 1)
accesslog = "-"
backlog = 500
bind = "0.0.0.0:8000"
statsd_host = os.environ.get("STASD_HOST")
workers = 3
max_requests = 2
"""
This module is the entry point for the API.
This will contain all of the middleware and routes and database initialization
"""
from starlette.applications import Starlette
from starlette.routing import Route
# from sample_project import HealthCheckAPI
from src.health_check import HealthCheckAPI
routes = [
Route(f"/health-check", HealthCheckAPI)
]
app = Starlette(debug=True, routes=routes)
# app = AsyncioWSGIMiddleware(app)
if __name__ == "__main__":
from hypercorn.config import Config
config = Config()
config.bind = ["localhost:8077"]
config.debug = True
import asyncio
from hypercorn.asyncio import serve
asyncio.run(serve(app, config))
hypercorn -c file:config/hypercorn.config.py src.application:app
Below is the sample config for the gunicorn server with the server hook.
import multiprocessing as mp
import os
print( (mp.cpu_count() * 2) + 1)
accesslog = "-"
backlog = 500
bind = "0.0.0.0:8000"
statsd_host = os.environ.get("STASD_HOST")
workers = 3
max_requests = 2
def on_starting(server):
# register some variables here
print(server)
print("Starting Flask application")
def on_exit(server):
# perform some clean up tasks here using variables from the application
print("Shutting down Flask application")
def worker_exit(server, worker):
print("Worker exiting")
print(worker)
def nworkers_changed(server, new_value, old_value):
print("n worker",new_value)
Hypercorn does not currently have any server hooks. However, on_starting and on_exit are accommodated for by the ASGI lifespan events. When using Starlette these are the on_startup and on_shutdown events.