python - Django - Celery Worker - 频道
问题描述
先感谢您
我正在尝试启动一个 Celery Worker 来接受使用频道的 WebConnections - 但是当我的工人启动时,它似乎无法找到频道。当我安装了 pip list channels
settings.py 有频道
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'channels','
任务.py
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, task.request.id)}
sync_thread = threading.Thread(target=sync_render, args=(bpy, context, reply_channel))
sync_thread.start()
bpy.ops.render.render()
context['rendering'] = False
sync_thread.join()
if os.path.exists(context['filepath']):
os.remove(context['filepath'])
if reply_channel is not None:
Channel(reply_channel).send({
'text': json.dumps({
'action': 'render_finished'
})
})'
我得到的错误 -
from channels import Channel
ImportError: cannot import name 'Channel' from 'channels'
(/usr/local/lib/python3.8/dist-packages/channels/__init__.py)
再次感谢您提前
解决方案
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'
})
})
推荐阅读
- python - 带有随机森林的 GridSearchCV - 无效参数错误
- python - 使用管理器后的 Python 多处理运行时错误
- vue.js - VUE (Cli) 和带 Node js 的 Stripe
- c++ - 计算器的双精度字符串转换
- r - data.table 中的连续天数按主题重置
- django - 使用 Cloud SQL PostgreSQL DB 在 Cloud Run 上的 Django 应用程序出错 - 无法连接到数据库
- php - Laravel:加入返回所有元素
- r - 计算 P 值的分布(对于 x 的所有可能值)
- javascript - 当一个函数接受一个包含等号的参数时,这意味着什么?
- c++ - 如何编写用于传递可变大小的匿名 std::array 的推导指南?