简体   繁体   English

Asyncio - 如何检测另一个任务中的结束任务

[英]Asyncio - How detect the end task in another task

How to detect the end or „is running” of a task that is running in another task.如何检测正在另一个任务中运行的任务的结束或“正在运行”。 How to detect termination or running task: some_function.run()?如何检测终止或正在运行的任务:some_function.run()?

async def service1():

    while True:
    ...
    ap_task = uasyncio.create_task(some_function.run())

    await uasyncio.sleep(0)

async def main():

    tasks = (service1, service2)
        res = await uasyncio.funcs.gather(*tasks, return_exceptions=False)

uasyncio.run(main())

When you create a task with create_task , you get a task object.当您使用create_task创建任务时,您会得到一个任务 object。

You can then get the status of the tasks using methods such as:然后,您可以使用以下方法获取任务的状态:

  • task.done()任务完成()
  • task.cancelled()任务.取消()
  • task.result任务.结果

See thefull documentation for details.有关详细信息,请参阅完整文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM