我有一个非常大的频道问题。当我尝试在生产中运行 asgi 服务器时出现问题,但在终端中运行时没有问题。
首先让我给你看一个小代码
class LogConsumer(AsyncConsumer):
async def websocket_connect(self, event):
print('befor')
await self.send({
"type": "websocket.accept",
"text": "hellow"
})
print('after')
async def websocket_disconnect(self, event):
print(event)
还有更多,但我也评论了它们,看看问题是否正在解决,然后猜猜是什么......
application = ProtocolTypeRouter({
'websocket': AllowedHostsOriginValidator(
AuthMiddlewareStack(
URLRouter(
[
url(r"^ws/monitoring/$", LogConsumer),
]
)
),
)
})
ASGI_APPLICATION = "fradmin_mainserver.routing.application"
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("localhost", 6379)],
},
},
}
ASGI_THREADS = 1000
主管配置
[fcgi-program:asgi]
socket=tcp://localhost:8008
environment=PYTHONPATH=/home/datis/.pyenv/versions/cv/bin/python
User=datis
environment=HOME="/home/datis",USER="datis"
# Directory where your site's project files are located
directory=/home/datis/PycharmProjects/fradmin_mainserver/
# Each process needs to have a separate socket file, so we use process_num
# Make sure to update "django_chanels.asgi" to match your project name
command=/home/datis/.pyenv/versions/cv/bin/daphne -u /run/uwsgi/daphne%(process_num)d.sock --fd 0 --access-log - --proxy-headers fradmin_mainserver.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/uwsgi/asgi.log
redirect_stderr=true
好的,这些是配置。
当我使用
daphne fradmin_mainserver.asgi:application --bind 0.0.0.0 --port 8008 --verbosity 1
没有问题,但是当在主管内部使用这个时,我唯一得到的是:
2021-04-13 11:45:27,015 WARNING Application instance <Task pending coro=<SessionMiddlewareInstance.__call__() running at /home/datis/.pyenv/versions/3.6.8/envs/cv/lib/python3.6/site-packages/channels/sessions.py:183> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f02e2222d38>()]>> for connection <WebSocketProtocol client=['127.0.0.1', 46234] path=b'/ws/monitoring/'> took too long to shut down and was killed.
即使我尝试使用正确的代码启动服务,我也做了:
[Unit]
Description=daphne daemon
After=network.target
[Service]
PIDFile=/run/daphne/pid
User=root
Group=root
WorkingDirectory=/home/datis/PycharmProjects/fradmin_mainserver/
Environment="DJANGO_SETTINGS_MODULE=fradmin_mainserver.settings"
ExecStart=/home/datis/.pyenv/versions/cv/bin/daphne --bind 0.0.0.0 --port 8008 --verbosity 0 fradmin_mainserver.asgi:application
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
Restart=on-abort
PrivateTmp=true
StandardOutput=file:/var/log/daphne/access.log
StandardError=file:/var/log/daphne/access.log
[Install]
WantedBy=multi-user.target
但结果是一样的:
它就像 websocket_connec() 永远不会被调用
我试图用syncconsumer创建它,但问题是一样的
但是当我停止 supervisorctl 所有运行
192.168.7.100:0 - - [13/Apr/2021:14:38:24] "WSCONNECTING /ws/monitoring/" - -
192.168.7.100:0 - - [13/Apr/2021:14:38:24] "WSCONNECT /ws/monitoring/" - -
before
192.168.7.100:0 - - [13/Apr/2021:14:39:25] "WSDISCONNECT /ws/monitoring/" - -
{'type': 'websocket.disconnect', 'code': 1001}
192.168.7.100:0 - - [13/Apr/2021:14:39:25] "WSCONNECTING /ws/monitoring/" - -
192.168.7.100:0 - - [13/Apr/2021:14:39:25] "WSCONNECT /ws/monitoring/" - -
before
192.168.7.100:0 - - [13/Apr/2021:14:39:27] "WSDISCONNECT /ws/monitoring/" - -
版本:python:3.6.8 django:2.2.6 channels:2.4.0 channels_redis:2.4.2 daphne:2.5.0
帮我取悦它一个真正的产品项目,我不再做什么我尝试了一切并阅读了堆栈溢出 github 等中的每一行。