[python,aiohttp] Don't create persistent aiohttp.ClientSession in __init__ #20292
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
aiohttp's
ClientSession
&TCPConnector
used to obtain an event loop in init (viaasyncio.get_event_loop
). However, as of aio-libs/aiohttp#8512 both classes now obtain the running event loop and won't potentially create one. This makes it impossible to createClientSession
andTCPConnector
objects outside of coroutines, asget_running_loop
must be called from a coroutine.Thus we defer the creation of a
ClientSession
into the actual request and cache it for later usage. Thereby we pay only a very small price on the first request, but subsequent requests will not be any more expensive.This PR should fix a long standing issue in py-gitea-opensuse-org: https://github.com/dcermak/py-gitea-opensuse-org/actions/runs/12208693521/job/34062321907?pr=48 which broke with a recent release of aiohttp. I have tested this PR to regenererate py-gitea-opensuse-org and with this PR, the tests pass
PR checklist
Read the contribution guidelines.
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
Run the following to build the project and update samples:
File the PR against the correct branch:
master
(upcoming7.x.0
minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
cc @cbornet @tomplus @krjakbrjak @fa0311 @multani