如何使用asyncio安排和取消任务 - python

我正在编写一个客户端服务器应用程序。连接后,客户端会每秒向服务器发送一个“心跳”信号。
在服务器端,我需要一种机制,可以在其中添加要异步执行的任务(或协程或其他东西)。而且,当客户端停止发送“心跳”信号时,我想从客户端取消任务。

换句话说,当服务器启动任务时,它具有某种超时或ttl,例如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()

python大神给出的解决方案

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

ensure_future将自动将协程包装在Task包装器中,并将其附加到事件循环中。然后,Task包装器还将确保协程从'cc>到await语句“滚动”(或直到协程完成)。

换句话说,只需将常规协程传递给await并将结果ensure_future对象分配给变量。然后,您可以在需要停止它时呼叫Task

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()

请注意,任务是为长时间运行的任务而设计的,这些任务需要在后台保持工作而不中断主流程。如果您需要的只是一种快速的一次性方法,则只需直接调用该函数即可。

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

Python:同时在for循环中添加到列表列表 - python

我想用for循环外的0索引值创建一个新列表,然后使用for循环添加到相同的列表。我的玩具示例是:import random data = ['t1', 't2', 't3'] masterlist = [['col1', 'animal1', 'an…

如何获取Python中所有内置函数的列表 - python

当我们从中获取关键字列表时,如何从Python提示符中获取Python中所有内置函数的列表? python大神给出的解决方案 更新:关于__builtins__或__builtin__可能会有一些混淆。What’s New In Python 3.0建议使用builtins 将模块__builtin__重命名为builtins(删除下划线, 添加一个“ s”…