我在官方文档的监督下运行daphne。由于某种原因,它一直失败,没有任何明显的错误以下是管理员日志
2021-07-18 15:35:01,110 INFO Creating socket tcp://localhost:8000
2021-07-18 15:35:01,112 INFO spawned: 'asgi0' with pid 15075
2021-07-18 15:35:01,116 INFO exited: asgi0 (exit status 0; not expected)
2021-07-18 15:35:01,116 INFO Closing socket tcp://localhost:8000
2021-07-18 15:35:04,128 INFO Creating socket tcp://localhost:8000
2021-07-18 15:35:04,130 INFO spawned: 'asgi0' with pid 15079
2021-07-18 15:35:04,134 INFO exited: asgi0 (exit status 0; not expected)
2021-07-18 15:35:04,134 INFO Closing socket tcp://localhost:8000
2021-07-18 15:35:05,136 INFO gave up: asgi0 entered FATAL state, too many start retries too quickly
这是管理员配置文件
[fcgi-program:asgi]
# TCP socket used by Nginx backend upstream
socket=tcp://localhost:8000
# environment=DJANGO_SETTINGS_MODULE=mysite.dev_settings
# Directory where your site's project files are located
directory=/home/jaga/C42
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "mysite.asgi" to match your project name
command=bash -c 'source /etc/environment' && daphne -u /run/daphne/daphne%(process_num)d.sock --fd 10 --access-log - --proxy-headers mysite.asgi:application
# Number of processes to startup, roughly the number of CPUs you have
numprocs=1
# Give each process a unique name so they can be told apart
process_name=asgi%(process_num)d
# Automatically start and recover processes
autostart=true
autorestart=true
# Choose where you want your log to go
stdout_logfile=/var/log/asgi.log
redirect_stderr=true
当有4个过程时也发生了同样的情况。
我应用了这个文档:django通道到生产,它就工作了。
请注意,您必须创建/run/daphne
目录,然后应用良好的权限。
mkdir -p /run/daphne && chown www-data:www-data
对于django,我的设置是:
[supervisord]
user=root
nodaemon=true
logfile=/dev/stdout
logfile_maxbytes=0
environment=
VIRTUAL_ENV=/srv/env
PATH="/srv/env/bin:%(ENV_PATH)s"
[fcgi-program:application]
user=www-data
group=www-data
directory=/var/www/application
socket=tcp://0.0.0.0:5000
process_name=asgi%(process_num)d
command=daphne -u /run/daphne/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers main.asgi:application
stdout_logfile=/var/log/application_stdout.log
redirect_stderr=true
作为额外的asgi.py
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings')
django.setup()
'''
Django is setuped.
Keep rows above, order is important.
All the line bellow works as standard project.
'''
from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
import main.routing as main_routing
print("Use ASGI server")
application = ProtocolTypeRouter(
{
"http": get_asgi_application(),
"websocket": AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
main_routing.websocket_urlpatterns
)
)
),
}
)