代码之家  ›  专栏  ›  技术社区  ›  Coding Dog

Python异步函数不工作。继续跑

  •  0
  • Coding Dog  · 技术社区  · 1 年前

    问题是运行代码时没有完成

    它只是打印索引并在末尾停止

    我做错了什么?

    limits = httpx.Limits(max_keepalive_connections=5, max_connections=10)
    
    finalList=[]
    async def getVouchDetails(link, client):
            ##getting params from link
            response = await client.get('https://egramswaraj.gov.in/paymentVoucherDetail.do',
                                    params=params, headers=headers,)
            soup = BeautifulSoup(response.content, 'lxml')
            return  soup
    
    async def poolVouch(link, client):
            print(links.index(link))
            soup = await getVouchDetails(link,client)
            ### extract data from soup
            finalList.append([##add required data])
    
    async def main(links):
        async with httpx.AsyncClient(limits=limits) as client:
            tasks = []
            for link in links:
                tasks.append(asyncio.ensure_future(poolVouch(link, client)))
            await asyncio.gather(*tasks)
    ##Links contains 100k links
    asyncio.run(main(links))
    

    它还在运行吗?我得等一下还是其他什么

    1 回复  |  直到 1 年前
        1
  •  -1
  •   LtGenFlower    1 年前

    1) 以下为:

    tasks.append(asyncio.ensure_future(poolVouch(link, client)))
    

    ->

    tasks.append(asincio.create_task(poolVouch(link, client)))
    await asyncio.sleep(0)