Django – Celery Worker – Channels

thank you in advance

I am trying to start a Celery Worker to accept WebConnections usng Channels – but when my worker starts it cannot seem to find channels. When I pip list channels is installed has channels


from __future__ import absolute_import, unicode_literals
import os
import threading
from merlin.celery import app, get_blender
from django.conf import settings
from celery import shared_task
from channels import Channel

@app.task(bind=True, track_started=True)
def render(task, data, reply_channel):
    bpy = get_blender()
    setup_scene(bpy, data)

context = {'rendering': True, 'filepath': os.path.join(settings.BLENDER_RENDER_TMP_DIR,}
sync_thread = threading.Thread(target=sync_render, args=(bpy, context, reply_channel))
context['rendering'] = False

if os.path.exists(context['filepath']):

if reply_channel is not None:
        'text': json.dumps({
            'action': 'render_finished'

The error I get –

from channels import Channel
ImportError: cannot import name 'Channel' from 'channels' 

Again thank you in advance

>Solution :

See the documentation for using channel layers outside of consumers

from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
channel_layer = get_channel_layer()

@app.task(bind=True, track_started=True)
def render(task, data, reply_channel):
    async_to_sync(channel_layer.send)(reply_channel, {
        'type': 'this_is_required',
        'text': json.dumps({
            'action': 'render_finished'

You may want to consider using channel workers and background tasks

Leave a Reply