django / asgi_ipc

IPC-based ASGI channel layer
BSD 3-Clause "New" or "Revised" License
37 stars 13 forks source link

Sending messages to channels via celery #28

Closed caronc closed 7 years ago

caronc commented 7 years ago

I'm experiencing a very similar problem to this one in my Django Development/Testing environment with the following configuration:

# settings.py
CHANNEL_LAYERS = {
    "default": {
        # This example app uses the Redis channel layer implementation
        # asgi_inmemory
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "mytest.routing.channel_routing",
    },
}

My Problem: A scheduled (Celery) task can not send a web socket request through channels. However, it works great in views and other locations. The symptoms are present both with the immediately=True setting and without it. There is no error or anything; the web socket request is just simply not passed along. However having a signal generated from within a model/view works awesome (so i know my setup is good - or so i think).

# from within a celery task (this will just silently fail and not fire to the client):
Group("test").send({
    "text": json.dumps({
        'type': 'test',
    })
}, immediately=True)

# nothing gets sent to the user at this point

I'm launching a celery worker (before starting Django) like so:

   DJANGO_SETTINGS_MODULE=myapp.settings celery --app=myapp worker --beat \
      -l debug \
      --concurrency=1 \
      -Q celery,testqueueA,testqueueB \
      --pidfile for_killing_later.pid &

Was just curious if there was something i was missing here? My package versions are:

Any help and/or advice would be most welcome!

andrewgodwin commented 7 years ago

Are you running inside of Docker?

andrewgodwin commented 7 years ago

Oh, wait, I've just seen you are using the inmemory channel layer - this does not work between processes (and you've also got the wrong repo anyway). Switch to the IPC or Redis layers and you'll be able to talk between processes.

caronc commented 7 years ago

i feel stupid now :blush: ; asgi_ipc is working as expected. Thank you for such a fast reply and pointing out my mistake!