小编Ane*_*del的帖子

asyncio on django RuntimeError: Task <Task pending coro=<future_timer() running got Future <Future pending> 附加到不同的循环

在 django 上使用命令行运行的 asyncio 上的任务。我已经设置了 celery 并且 celery 正在调用函数来在 asyncio 上创建循环,它正在调用等待 asyncio 的函数,但它给出了以下错误。

yield self # 这告诉 Task 等待完成。运行时错误:任务 > 将 Future 附加到不同的循环。

我在 wsgi 上使用 python3 运行 django,在守护进程上运行 celery。以下是代码示例。

import asyncio
import datetime


@asyncio.coroutine
def slow_operation(name, sec):
    message = '%s started at %s' % (name, datetime.datetime.now())
    print(message)
    yield from asyncio.sleep(sec)
    return '%s ended at %s after %s seconds' % (name, datetime.datetime.now(), sec)


def got_result(future):
    print(future.result())


def run_process(name, sec):
    task = loop.create_task(slow_operation(name, sec))
    task.add_done_callback(got_result)

loop = asyncio.get_event_loop()

i = [{'name': …
Run Code Online (Sandbox Code Playgroud)

python django python-asyncio

5
推荐指数
0
解决办法
978
查看次数

标签 统计

django ×1

python ×1

python-asyncio ×1