【问题标题】:Django signals not working with channels in multicontainer setupDjango 信号不适用于多容器设置中的通道
【发布时间】:2018-05-30 06:52:57
【问题描述】:

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

您可以在github 上查看信号的定义方式。

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

docker-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】:

    我的问题是信号没有加载,因为我在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 *
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      相关资源
      最近更新 更多