Django信号在多容器设置中不使用通道

问题描述 投票:2回答:1

我有django app并使用channelschannels api实现websocket支持。我正在使用解复用器绑定到我的模型。例如,当我保存模型时,它会将更改发送到我的开放websocket连接。如果我运行./manage.py runserver 0:80并将所有内容放在一个容器中,一切正常。但是,如果我将我的应用程序分离到UWSGI,daphne和使用docker的工作容器,则不会触发信号。例如,我希望任何芹菜工人(任务)触发信号并通过websocket发送更新。在我的多容器设置中,websocket连接建立好,Web工作正常,但没有任何触发信号。

如何定义信号,你可以在github上看到。

我正在使用django 1.9.12,python 2.7,docker并在debian stretch上构建。

泊坞窗,compose.yml

  web:
    build: .
    ports: ["8001:80"]

  daphne:
    build: .
    command: daphne -b 0.0.0.0 -p 8000 --access-log - -v 2 my_proj.asgi:channel_layer 

  ws_worker:
    build: .
    command: ./manage.py runworker -v 2

  celery_worker:
    build: .
    command: /usr/local/bin/celery -A my_proj worker

nginx.conf

upstream django {
    server unix:/home/docker/app.sock; 
}

server {
    listen 80;
    server_name 127.0.0.1;
    charset utf-8;
    client_max_body_size 1000M;

    location /static/ {
        alias /home/docker/static/;
    }

    # proxy to other container
    location /ws/ {
        proxy_pass http://daphne:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    location / {
        uwsgi_pass  django;
        include     /home/docker/uwsgi_params;
    }

}
python django docker django-channels daphne
1个回答
0
投票

我的问题是信号没有加载导致我定义了绑定类别,而不是models.py。如果我在模型加载到my_app / config.py后加载它们,它可以跨多个容器工作

from django.apps import AppConfig as DefaultAppConfig

class AppConfig(DefaultAppConfig):
    def ready(self):
        # for websockets bindigns
        from my_app.websockets.bindings_one import *
        from my_app.websockets.bindings_two import *
© www.soinside.com 2019 - 2024. All rights reserved.