有机会屏蔽create_task的任务吗?

问题描述

我需要屏蔽在aiohttp处理程序进程中被create_task触发的任务

async def handler(request):
    asyncio.create_task(long_process())
    return {},200

所以代码

import asyncio


async def shielded(coro):
    try:
        await asyncio.shield(coro)
    except asyncio.CancelledError:
        await coro
        raise


def create_task(coro) -> asyncio.Task:
    task = asyncio.create_task(shielded(coro))
    return task

但是这个测试

async def test_create_task_cancel():
    async def coro():
        await asyncio.sleep(1)
        return None

    task = create_task(coro())
    await asyncio.sleep(0.1)
    task.cancel()
    await asyncio.sleep(1)

    assert task.done() and task.result() is None

使用

RuntimeError: coroutine is being awaited already

aiohttp手册建议使用aiojobs.Scheduler,但无法正常工作 https://github.com/aio-libs/aiojobs/issues/148 https://github.com/aio-libs/aiojobs/issues/72

解决方法

工作屏蔽功能

return ClipRRect(
      borderRadius: BorderRadius.circular(16.0),child: Column(
        mainAxisSize: MainAxisSize.min,children: <Widget>[
          topContent,bottomContent,],),);