协程从未等待,异步多线程

时间:2020-10-21 14:15:49

标签: python-3.x python-asyncio python-multithreading aiohttp

async def existance(s, name):
    async with s.head(f"https://example.com/{name}") as r1:
        if r1.status == 404:
            print('wow i worked')

async def process(names):
    with ThreadPoolExecutor(max_workers=3) as executor:
        async with aiohttp.ClientSession() as s:
            loop = asyncio.get_event_loop()
            tasks = []
            for name in names:
                if len(name) >= 5 and len(name) < 16 and name.isalnum():
                    task = loop.run_in_executor(
                        executor,
                        existance,
                        *(s, name)
                        )
                    tasks.append(task)
            return await asyncio.gather(*tasks)

while True:
    start_time = time.time()
    loop = asyncio.get_event_loop()
    future = asyncio.ensure_future(process(names))
    loop.run_until_complete(future)

我正在使用上面的代码尝试在多个线程中拆分创建的任务,同时异步检查所有任务。

我收到此错误:

RuntimeWarning: coroutine 'existance' was never awaited
  future = asyncio.ensure_future(process(names))

我还是一个python初学者,我无法真正弄清楚我应该在这里进行哪些更改以获得我想要的结果。 感谢您的帮助,如果这是一个重复的问题,我们将感到抱歉。

1 个答案:

答案 0 :(得分:0)

由于existance是异步的,因此根本不需要线程,因此可以像这样实现process

async def process(names):
    async with aiohttp.ClientSession() as s:
        tasks = []
        for name in names:
            if len(name) >= 5 and len(name) < 16 and name.isalnum():
                tasks.append(existance(s, name))
        return await asyncio.gather(*tasks)

如果existance正在调用某个可能会阻塞事件循环的同步慢函数(例如BeautifulSoup解析),则可以通过在线程池中仅运行那个函数来解决此问题。像这样在本地引入run_in_executor

# instead of:
# result = slow_calculation(arg1, arg2)   # sync/slow code
# use this:
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, slow_calculation, arg1, arg2)

但是不要尝试通过run_in_executor调用 async 函数,这是行不通的。