2

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

信号是如何定义的,您可以在github上查看。

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

码头工人-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;
    }

}
4

1 回答 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 *
于 2017-12-18T17:14:01.957 回答