第一波后Asyncio信号量卡住

我正在尝试使用Semaphore异步获取一堆网站,包括防止呈现页面(设置为5)时发生超时异常。

我已经测试了pages列表中的几个页面的程序,这些页面返回了预期的结果,但是由于渲染操作期间发生了超时异常,因此添加页面时程序开始失败。因此,我决定将Semaphore添加到程序中。

async def get_page(assesion,page,semaphore):
    async with semaphore:
        logging.info(f"Processing page: {page}")
        response = await assesion.get(prop)
        try:
            await response.html.arender(timeout=3000)
        except:
            response = None
        return response

async def main(pages):
    asession = requests_html.AsyncHTMLSession()
    semaphore = asyncio.Semaphore(5)
    tasks = [asyncio.ensure_future(get_property(asession,semaphore)) for page in pages]
    return await asyncio.gather(*tasks)

loop = asyncio.get_event_loop()
loop.set_debug(True)
results = loop.run_until_complete(main(pages))
print(results)

在当前状态下,程序从pages列表中提取前5页,然后以铬打开它们,然后显示Processing page: ...列表中其他页面的pages(不显示打开记录的日志)页)。在[I:pyppeteer.connection] connection closed末尾五次(可能是关闭前五页)。

tftp156656 回答:第一波后Asyncio信号量卡住

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3128763.html

大家都在问