You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a quite significant performance degradation when cookies are being used with requests.
httpx._client.BaseClient._merge_cookies calls into the cookiejar here:
if cookies or self.cookies:
merged_cookies = Cookies(self.cookies)
merged_cookies.update(cookies)
return merged_cookies
return cookies
When either cookies or self.cookies is not None, then httpx._models.Cookies.__bool__ will call into a method called deepvalues in http.cookiejar.CookieJar by indirectly calling http.cookiejar.CookieJar.__iter__.
httpx._models.Cookies.__bool__:
def __bool__(self) -> bool:
for _ in self.jar:
return True
return False
Pulling in the stdlib http.cookiejar and urllib.request modules brings in quite a lot of submerged complexity... I'd wonder if we really need that for the httpx.Cookies implementation.
Could we instead be using a plain dictionary format under-the-hood for the cookie persistence?
Which cookie attributes does httpx honor? For example, does "expires" actually take affect?
Can we point to a really clear easy-to-read functional description of http cookies?
Users may want to access it in order to customize the cookie policy (which is set during initialization and accessed when adding a header). So a compatibility layer may be warranted, and it can get complex.
Other than enforcing a cookie policy, the CookieJar appears to be mostly concerned about thread-safety, and it could be replicated in the Cookies class.
Which cookie attributes does httpx honor? For example, does "expires" actually take affect?
With #3322 I removed _merge_cookies. There is another place in httpx.Request that bool check for cookies. @gyula-lakatos Do you able to reproduce with #3322 ?
Activity
perf: using None checking cookies instead of bool(encode#2875)
lovelydinosaur commentedon Nov 3, 2023
Pulling in the stdlib
http.cookiejarandurllib.requestmodules brings in quite a lot of submerged complexity... I'd wonder if we really need that for thehttpx.Cookiesimplementation.httpxhonor? For example, does "expires" actually take affect?vergeev commentedon Nov 5, 2023
I think the biggest obstacle here is the fact that the
jarattribute ofCookiesclass is a part of a public interface:httpx/docs/api.md
Line 154 in fbe35ad
Users may want to access it in order to customize the cookie policy (which is set during initialization and accessed when adding a header). So a compatibility layer may be warranted, and it can get complex.
Other than enforcing a cookie policy, the
CookieJarappears to be mostly concerned about thread-safety, and it could be replicated in theCookiesclass.It's
domain,pathandexpires. Thedomainandpathare tested, andexpiresis enforced at theCookieJarlevel. Each timehttpx.Cookies.set_cookie_headeris called, it callshttp.cookiejar.CookieJar. add_cookie_header, which clears the expired cookies: https://github.com/python/cpython/blob/24b5cbd3dce3fe37cdc787ccedd1e73a4f8cfc3c/Lib/http/cookiejar.py#L1387.T-256 commentedon Sep 27, 2024
With #3322 I removed
_merge_cookies. There is another place inhttpx.Requestthat bool check forcookies.@gyula-lakatos Do you able to reproduce with #3322 ?