小编Olj*_*jko的帖子

Python异步协程执行时间

我正在尝试测量 python 异步函数执行时间。但是我遇到了问题,因为函数启动的实际点是未知的。

测量功能代码:

def timer(func):
    async def process(func, *args, **params):
        if asyncio.iscoroutinefunction(func):
            print('this function is a coroutine: {}'.format(func.__name__))
            return await func(*args, **params)
        else:
            print('this is not a coroutine')
            return func(*args, **params)

    async def helper(*args, **params):
        print('{}.time'.format(func.__name__))
        start = datetime.datetime.now()
        print(start)

        result = await process(func, *args, **params)

        finish = datetime.datetime.now()
        print('>>> calculated - ', finish - start, 'start-', start, 'finish-', finish)
        return result

    return helper
Run Code Online (Sandbox Code Playgroud)

休息代码:

@timer
async def fetch(name, session):
    url = 'http://localhost:8808/'
    payload = {}

    async with session.put(url, …
Run Code Online (Sandbox Code Playgroud)

measurement python-3.x async-await python-asyncio aiohttp

5
推荐指数
1
解决办法
5524
查看次数