python中普通函数调用协程

import asyncio


def target(loop, timeout=None):
    future = asyncio.run_coroutine_threadsafe(add(1, b=2), loop)
    return future.result(timeout)


async def add(a, b):
    await asyncio.sleep(1)
    return a + b


loop = asyncio.get_event_loop()
future = loop.run_in_executor(None, target, loop)
print(loop.run_until_complete(future))
posted @ 2019-11-26 10:29  公众号python学习开发  阅读(1040)  评论(0编辑  收藏  举报