hpp
hpp

Reputation: 11

python sanic framework Listeners doesn't work

from loguru import logger
from sanic import Sanic

app = Sanic(__name__)

@app.before_server_start
async def start(*_):
    logger.info("Server starting...")

@app.before_server_stop
async def before_server_stop(*_):
    logger.info("Server stopping...")

@app.after_server_stop
async def after_stop(app, loop):
    logger.info("Server shutting down...")

@app.route("/")
async def test(request):
    return text("test")

if __name__ == '__main__':
    try:
        app.run(host='0.0.0.0', port=8000, workers=1)
    except KeyboardInterrupt:
        app.stop()

I tried running the above code, when I use Ctrl c stop server, two listeners: before_server_stop,after_server_stop doesn't work. The console only outputs:

2024-09-10 16:45:23.567 | INFO     | __mp_main__:start:20 - Server starting...

How can I solve this problem?

Upvotes: 1

Views: 30

Answers (0)

Related Questions