Restart Sanic from an external Python script?

Hi!

I noticed that when running the Sanic server, it seems to gracefully restart without killing the running requests (please correct me if I’m wrong).

Having a watcher for file changes ensure that a new instance is spawned, and made primary once the old one is stopped.

This is really nice and I tried looking through the code to find a way to call that “restart” part from an external code, but couldn’t find how to properly do that.

My issue is as follow:

When a push is done on Github, a (Python) script is run on my server. This script updates the code from the latest changes, upgrade the package and add new ones if any, then update the database (via alembic migration).

The issue is that right now, Sanic server is running using auto_reload=True. When a git pull is done, the server is restarted quickly, but if the new code contains a new package not yet installed, it fails.

What I would like to do is set auto_reload to False, be able to do my local changes (git pull, updating packages and database) then calling restart on Sanic like the auto_reload would do.

How could I go on calling restart on an already Sanic instance from an external script ?

Thanks in advance for your help!

This is why it is recommended to not do this in production. I can’t recommend that you use the method you’re attempting, but here’s a good place to start:

Thank you for your help.

Can you clarify what is not recommended ? Is it the “auto_reload” set to true, or something else ?

The idea here is exactly to remove that “auto_reload” and handle this in the code, when a push to prod is made.

If you don’t run the project in development mode or specifically set auto_reload to true, it should not be auto reloading at all.