如何用asyncio安排一个任务在executor中运行?

时间:2022-10-16 12:14:59

I have been using asyncio for concurrency, however I have run into a problem. I have the need to schedule a task with asyncio but it is blocking and so I would like to execute it in an executor using threadpool from concurrent.futures.

我一直在使用asyncio进行并发,但是我遇到了一个问题。我需要用asyncio来调度一个任务,但是它是阻塞的,所以我想用concurrent.futures的threadpool来执行它。

I have seen example like this, that schedule tasks:

我看到过这样的例子,计划任务:

now = loop.time()
loop.call_at(now + 60, callback, arg, loop)

and like this that run tasks in executors:

像这样在执行者中执行任务:

blocking_tasks = [
    loop.run_in_executor(executor, blocks)
    for i in range(6)
]
completed, pending = await asyncio.wait(blocking_tasks)

But how can I schedule a blocking task to run in executor?

但是我如何安排一个阻塞任务在executor中运行呢?

1 个解决方案

#1


2  

run_in_executor returns a future, so you can't use it with call_at, which requires an ordinary function. However, you can easily postpone execution using asyncio.sleep():

run_in_executor返回一个未来,因此不能对call_at使用它,这需要一个普通的函数。但是,可以使用asyncio.sleep()轻松地推迟执行:

async def my_task():
    await asyncio.sleep(60)
    result = await loop.run_in_executor(None, fn)
    ...

taskobj = loop.create_task(my_task())

This has the advantage that the task created by create_task can be canceled during the sleep. Also, you can return a useful value from my_task() and obtain it using await taskobj, calling taskobj.result(), or loop.run_until_complete(taskobj).

这样做的好处是create_task创建的任务可以在睡眠期间取消。此外,您还可以从my_task()返回一个有用的值,并使用wait taskobj、调用taskobject .result()或loop.run_until_complete(taskobj)获取它。

#1


2  

run_in_executor returns a future, so you can't use it with call_at, which requires an ordinary function. However, you can easily postpone execution using asyncio.sleep():

run_in_executor返回一个未来,因此不能对call_at使用它,这需要一个普通的函数。但是,可以使用asyncio.sleep()轻松地推迟执行:

async def my_task():
    await asyncio.sleep(60)
    result = await loop.run_in_executor(None, fn)
    ...

taskobj = loop.create_task(my_task())

This has the advantage that the task created by create_task can be canceled during the sleep. Also, you can return a useful value from my_task() and obtain it using await taskobj, calling taskobj.result(), or loop.run_until_complete(taskobj).

这样做的好处是create_task创建的任务可以在睡眠期间取消。此外,您还可以从my_task()返回一个有用的值,并使用wait taskobj、调用taskobject .result()或loop.run_until_complete(taskobj)获取它。