Django + Daphne hot reload after code updates
Asked Answered
A

3

7

I am running a Django restserver application served by Daphne and Nginx acting as reverse proxy. I also have a periodic cron job that pulls updated code from my git to the server.

I am not able to find a way to do a hot reload and regenerate the pyc files like it how it does in Django development server. Is there a good way to go about this?

Don't want to restart my entire server for this.

Afrit answered 27/3, 2018 at 1:11 Comment(0)
M
2

This is not supported yet. There is an open issue here: https://github.com/django/daphne/issues/9

Mellen answered 13/6, 2018 at 10:46 Comment(1)
yeah, true and reading the github issue it will never be supported. But uvicorn and hypercorn do :'-)Northbound
A
1

In the docker container, by ending the process and then restarting

$ ps ax
  PID TTY      STAT   TIME COMMAND
    1 ?        Ssl    0:01 /usr/local/bin/python /usr/local/bin/daphne -b 0.0.0.0 -p 8000 proj.asgi:application
    8 pts/0    Ss     0:00 /bin/sh
   19 pts/0    S      0:00 bash
   20 pts/0    R+     0:00 ps ax

$ kill 9 1
Archespore answered 7/1, 2022 at 2:46 Comment(0)
A
0

In one of our application, i was able to run Django with Daphne in hot reload mode (in my case in a docker environment), following this documentation on the official Django site: https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/daphne/#integration-with-runserver

Versions used:
Daphne = 4.0 (check release notes https://github.com/django/daphne/blob/main/CHANGELOG.txt)
Django = 3.2.13 (Versions > 3.0 should work; python > 3.6 is required)

One needs to integrate Daphne with the runserver command. This can be achieved by adding daphne explicitly to the INSTALLED_APPS of your django settings file (was not required in older daphne versions):

INSTALLED_APPS = [
    "daphne",
    ...,
] 

Create or edit your asgi.py file in your django root app:

import os

import django
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app.settings")

# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()

from my_app.routing import websockets

django.setup()
application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": websockets
})

Note: I am also using websockets in my app. In your app you may not need "websocket": websockets in the snippet above.

Start the development server from your application root path with the command:
python manage.py runserver

On the server startup you should see a log like this:

Django version 3.2.13, using settings my_app.settings.development    
Starting ASGI/Daphne version 4.0.0 development server at http://0.0.0.0:8000/

Now a daphne server should be running in your development environment and restart on every code change.

Hope this helps someone.

Assize answered 3/4 at 8:56 Comment(2)
More information required: How did you get hot-reloading to work with Django and Daphne? - Please give steps and code examples to help someone reproduce your success.Mello
How robust was your testing getting this working? - the IP address used in the server start line is a catch-all listening address suitable for development testing on a local machine but is unsuitable for production deployment (potentially with exceptions for containerised environments such as Docker?)Mello

© 2022 - 2024 — McMap. All rights reserved.