[Django]-Is it possible to start Daphne in a python script without using an OS call

2👍

This is kind of a work around, but you may use the subprocess module like this:

subprocess.run(["daphne", "-b 0.0.0.0 -p 8001 django_project.asgi:channel_layer"])

Check this thread Calling an external command in Python for more info on the use of subprocess module.

1👍

I am not familiar with Django Channels but have you tried using inmemory or redis Channels directly? You might be able to avoid daphne altogether. From what I understand, daphne seems to be the protocol translator layer so outside clients can communicate with Django through daphne (Django uses wsgi rather than asgi so Django alone cannot handle certain protocols, such as, websocket communication). Tornado does not rely on wsgi.

There are examples in daphne tests:
https://github.com/django/daphne/blob/master/daphne/tests/test_http.py

The inmemory ChannelLayer is not cross-process. I’m not sure if that even matters in your use case. If it does, you can check the other backends (such as redis channels) https://channels.readthedocs.io/en/stable/backends.html

This might be more directly what you are looking for:
https://github.com/django/asgi_redis

👤Louis

0👍

have you tried it swap the tornado to NGINX ?

http://masnun.rocks/2016/11/02/deploying-django-channels-using-daphne/

0👍

I ran into this same issue…

from daphne.cli import CommandLineInterface
CommandLineInterface().run(('my.asgi', '--bind', ip_address, '--port', '8000'))

Should do the trick. The trouble is that I don’t see this documented anywhere and so the API may not be very stable.

This is the same code route as calling Daphne through subprocess:
https://github.com/django/daphne/blob/main/daphne/cli.py#L167
https://github.com/django/daphne/blob/main/daphne/cli.py#L204

Leave a comment