如何使用Asyncio计划和取消任务 [英] How to schedule and cancel tasks with asyncio

查看:26
本文介绍了如何使用Asyncio计划和取消任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个客户端-服务器应用程序。在连接时,客户端向服务器发送"心跳"信号,例如,每秒。 在服务器端,我需要一种可以添加要异步执行的任务(或协程或其他内容)的机制。此外,当客户端停止发送"心跳"信号时,我想取消来自客户端的任务。

换句话说,当服务器启动一项任务时,它会超时或ttl,例如3秒。当服务器接收到"心跳"信号时,它会重新设置计时器3秒,直到任务完成或客户端断开连接(停止发送信号)。

这里是从pymotw.com上的asyncio教程中取消任务的example。但是在这里,任务在EVENT_LOOP启动之前被取消,这不适合我。

import asyncio

async def task_func():
    print('in task_func')
    return 'the result'


event_loop = asyncio.get_event_loop()
try:
    print('creating task')
    task = event_loop.create_task(task_func())

    print('canceling task')
    task.cancel()

    print('entering event loop')
    event_loop.run_until_complete(task)
    print('task: {!r}'.format(task))
except asyncio.CancelledError:
    print('caught error from cancelled task')
else:
    print('task result: {!r}'.format(task.result()))
finally:
    event_loop.close()

推荐答案

可以使用asyncioTask包装器通过ensure_future()方法执行任务。

ensure_future将自动将协程包装在Task包装器中,并将其附加到事件循环。然后,Task包装器还将确保协同例程从await语句到await语句(或直到协同例程结束)。

换句话说,只需将一个常规协程传递给ensure_future,并将得到的Task对象赋给一个变量。然后,您可以在需要停止时调用Task.cancel()

import asyncio

async def task_func():
    print('in task_func')
    # if the task needs to run for a while you'll need an await statement
    # to provide a pause point so that other coroutines can run in the mean time
    await some_db_or_long_running_background_coroutine()
    # or if this is a once-off thing, then return the result,
    # but then you don't really need a Task wrapper...
    # return 'the result'

async def my_app():
    my_task = None
    while True:
        await asyncio.sleep(0)

        # listen for trigger / heartbeat
        if heartbeat and my_task is None:
            my_task = asyncio.ensure_future(task_func())

        # also listen for termination of hearbeat / connection
        elif not heartbeat and my_task:
            if not my_task.cancelled():
                my_task.cancel()
            else:
                my_task = None

run_app = asyncio.ensure_future(my_app())
event_loop = asyncio.get_event_loop()
event_loop.run_forever()

请注意,任务用于需要在不中断主流的情况下保持后台工作的长时间运行的任务。如果您所需要的只是一个快速的一次性方法,那么只需直接调用该函数即可。

这篇关于如何使用Asyncio计划和取消任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆