[英]Examples of using authlib's httpx client asynchronously
There are several examples here of using an httpx
client instead of a requests
based session with the popular oauth lib, authlib
这里有几个示例使用
httpx
客户端而不是基于requests
的 session 和流行的 oauth lib、 authlib
However in those examples, they don't show how to properly open and close an async httpx
session.但是在这些示例中,它们没有显示如何正确打开和关闭异步
httpx
session。 See https://www.python-httpx.org/async/见https://www.python-httpx.org/async/
When I try to use it as suggested, I get warnings either about the session not being closed:当我尝试按照建议使用它时,我收到有关 session 未关闭的警告:
UserWarning: Unclosed <authlib.integrations.httpx_client.oauth2_client.AsyncOAuth2Client object at 0x000001B6444BFEB0>.
用户警告:未关闭 <authlib.integrations.httpx_client.oauth2_client.AsyncOAuth2Client object 在 0x000001B6444BFEB0>。 Seehttps://www.python-httpx.org/async/#opening-and-closing-clients for details
有关详细信息,请参阅https://www.python-httpx.org/async/#opening-and-closing-clients
And if I call twice, I get如果我打电话两次,我得到
RuntimeError: Event loop is closed
RuntimeError:事件循环已关闭
This makes complete sense to me, as the examples in authlib
s docs aren't using a context manager for the async session这对我来说完全有意义,因为
authlib
的文档中的示例没有使用异步 session 的上下文管理器
authlib
's AsyncOAuth2Client
inherits from httpx
's AsyncClient
, so you should be able to use the same methods given athttps://www.python-httpx.org/async/#opening-and-closing-clients . authlib
的AsyncOAuth2Client
继承自httpx
的AsyncClient
,因此您应该能够使用https://www.python-httpx.org/async/#opening-and-closing-clients给出的相同方法。 So either something like:所以要么像:
async with authlib.integrations.httpx_client.oauth2_client.AsyncOAuth2Client() as client:
...
or:或者:
client = authlib.integrations.httpx_client.oauth2_client.AsyncOAuth2Client()
...
await client.aclose()
Should allow you to open and close sessions as needed.应该允许您根据需要打开和关闭会话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.