Asynchronous 在异步django通道websocket中使用同步芹菜回调

Asynchronous 在异步django通道websocket中使用同步芹菜回调,asynchronous,websocket,celery,django-channels,python-asyncio,Asynchronous,Websocket,Celery,Django Channels,Python Asyncio,在我们的django通道异步使用者中,我试图检索一个组结果。这个结果可能是完整的,也可能不是完整的,因此我想在管道结果准备就绪时添加一个回调。问题是,websocket是异步的,回调必须是同步的,self.send方法必须是异步的。起初,我假设可以将异步发送包装成async\u-to\u-sync,尽管它会给出错误,但您不能在异步事件循环的同一线程中使用AsyncToSync-只需直接等待async函数。,因此,似乎不允许使用async->sync->async class ImageConsu

在我们的django通道异步使用者中,我试图检索一个组结果。这个结果可能是完整的,也可能不是完整的,因此我想在管道结果准备就绪时添加一个回调。问题是,websocket是异步的,回调必须是同步的,
self.send
方法必须是异步的。起初,我假设可以将异步发送包装成
async\u-to\u-sync
,尽管它会给出错误
,但您不能在异步事件循环的同一线程中使用AsyncToSync-只需直接等待async函数。
,因此,似乎不允许使用async->sync->async

class ImageConsumer(AsyncWebsocketConsumer):

    async def celery_retrieve_group_result(self, batch_id):
        group = group = celery.GroupResult().restore(batch_id)
        res = group.get(callback=self.sync_callback)

    def sync_callback(self, task_id, value):
        async_to_sync(self.send_group_result)(task_id, value)

    async def send_group_result(self, task_id , value):
        await self.send(json.dumps({"type": "result.redraw_border", "result": value}))
我尝试了不同的组合,尽管我的限制因素是回调必须是同步的,而其他的都是异步的。 有没有人有过以这种方式将芹菜与async django混合的经验?
任何帮助都将不胜感激

决定采用一种不同的方法对封闭的异步结果进行迭代

class ImageConsumer(AsyncWebsocketConsumer):
    name = "Image Consumer"
    timeout = 20
    sleep_duration = 5e-2

    async def celery_retrieve_group_result(self, batch_id):

        group = celery.GroupResult().restore(batch_id)
        if group is not None:
            ordered_list_of_async_results = group.results
            for result in ordered_list_of_async_results:
                start = time.time()
                try:
                    while not result.ready():
                        await asyncio.sleep(self.sleep_duration)
                        if self.timeout and time.time() - start > self.timeout:
                            logger.exception(str(TimeoutError))
                            raise TimeoutError
                    value = result.get()
                    await self.send(json.dumps({'type': 'result.processing', 'result': value}))
                except:
                    await self.send(json.dumps({'type': 'result.processing', 'error': str(TimeoutError)}))
            group.forget()
        else:
            await self.send(json.dumps({'type': 'response.nonexistant_group', 'error': 'No result batch exists. re-upload'}))
这真的很有效。超时是因为芹菜4.3当前在Redis后端上存在问题,如果删除,将导致死锁。所以,在这个问题得到解决之前,这一切都是完美的