django / channels

Developer-friendly asynchrony for Django
https://channels.readthedocs.io
BSD 3-Clause "New" or "Revised" License
6.1k stars 800 forks source link

Workers interfering with eachother #391

Closed deshraj closed 8 years ago

deshraj commented 8 years ago

I am running two Django projects on my staging server. Both of them are using channels. I am running two daphne servers each for one. When I start the workers using runworker of both the projects, then I can see that the worker of one project seems to consume the requests of the other project and vice versa and causing problems.

The two daphne servers are running at different ports 8004 and 9000. OS: Ubuntu 14.04 Versions: asgi-redis==0.14.1 asgiref==0.14.0 autobahn==0.16.0 backports.ssl-match-hostname==3.5.0.1 channels==0.17.2 daphne==0.15.0 Django==1.10.1 pika==0.10.0 protobuf==3.0.0 redis==2.10.5 six==1.10.0 Twisted==16.4.1 txaio==2.5.1 uWSGI==2.0.13.1 websocket-client==0.37.0 zope.interface==4.3.2

Also, I am using nginx as webserver. The nginx configuration for both the projects are as follows:

Project 1

upstream gradcam {
    server 127.0.0.1:8001;
    }

server {
    listen      80;
    charset     utf-8;
    client_max_body_size 75M;   # adjust to taste

    location /media  {
        alias /path/to/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/static; # your Django project's static files - amend as required
    }
    location /chat {
        proxy_pass          http://127.0.0.1:9000;
        proxy_http_version  1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection "Upgrade";
    }

    location / {
        uwsgi_pass  gradcam;
        include     /path/to/uwsgi_params; # the uwsgi_params file you installed
        proxy_http_version  1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection "Upgrade";
    }
}

Project 2

upstream svqa {
    server 127.0.0.1:8003; # for a web port socket (we'll use this first)
}

server {
    listen      80;
    server_name abc.xyz.com;
    charset     utf-8;

    client_max_body_size 75M;   # adjust to taste

    location /media  {
        alias /path/to/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/static; # your Django project's static files - amend as required
    }   

    location /chat {
        proxy_pass          http://127.0.0.1:8004;
        proxy_http_version  1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection "Upgrade";
    }

    location / {
        uwsgi_pass  svqa;
        include     /path/to/uwsgi_params; # the uwsgi_params file you installed
        proxy_http_version  1.1;
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection "Upgrade";
    }
}
andrewgodwin commented 8 years ago

My suspicion is that both projects are sharing the same ASGI backend - you need to set one to have a different value for prefix in its configuration, otherwise, yes, they will all connect to each other and share requests.

deshraj commented 8 years ago

@andrewgodwin Thanks for the quick reply. I can show you the current channel_layers config for both the projects.

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
        "ROUTING": "vicki.routing.channel_routing",
    },
}

and

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
        },
        "ROUTING": "gradcam.routing.channel_routing",
    },
}

Can you please point me to some link or send me a sample config code?

andrewgodwin commented 8 years ago

Yup, they are the same configuration and so they are talking to each other. Docs for asgi_redis are at https://github.com/django/asgi_redis/blob/master/README.rst, but in short, you should add a prefix like so (different for each project):

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [("localhost", 6379)],
            "prefix": "gradcam",
        },
        "ROUTING": "gradcam.routing.channel_routing",
    },
}
deshraj commented 8 years ago

Thanks @andrewgodwin . Channels is working like charm for my projects. :smile: