如何用httpx库的AsyncClient 实现使用同一个session连接进行请求?

有2个不同功能的爬虫函数,功能无耦合。
他们分别向同一个网站发出http请求。

我想使用异步httpx库的AsyncClient ,以使两个函数使用同一个session连接来进行请求。
请问以下两种写法是否都一样达到了上述的效果?谢谢

写法1:

async def a():
  async with httpx.AsyncClient() as client:
    response = client.get(xxxx)
    return response 
    
async def b():
  async with httpx.AsyncClient() as client:
    response = client.get(xxxx)
    return response 

写法2:

async def a(client:httpx.AsyncClient()):
  response = client.get(xxxx)
  return response 

async def b(client:httpx.AsyncClient()):
  response = client.get(xxxx)
  return response 

async with httpx.AsyncClient() as client:
  response1 = await a(client)
  response2 = await b(client)

答案是肯定的,两种写法都可以实现你想要的功能。第一种方法使用两个不同的AsyncClient实例,而第二种方法使用相同的AsyncClient实例,由 async with语句管理。

有其他大佬有新的答案吗? 求指点