[python,aiohttp] Don't create persistent aiohttp.ClientSession in __init__#20292
Merged
wing328 merged 1 commit intoOpenAPITools:masterfrom Dec 15, 2024
Merged
Conversation
fbf31cf to
7d33482
Compare
…nit__ aiohttp's `ClientSession` & `TCPConnector` used to obtain an event loop in __init__ (via `asyncio.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 create `ClientSession` and `TCPConnector` objects outside of coroutines, as `get_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.
7d33482 to
152f83a
Compare
Member
|
given that you've tested it in your use cases to confirm the fix, we can give it a try. thanks for the PR |
timon-sbr
pushed a commit
to timon-sbr/openapi-generator
that referenced
this pull request
Mar 13, 2025
…nit__ (OpenAPITools#20292) aiohttp's `ClientSession` & `TCPConnector` used to obtain an event loop in __init__ (via `asyncio.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 create `ClientSession` and `TCPConnector` objects outside of coroutines, as `get_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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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&TCPConnectorused 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 createClientSessionandTCPConnectorobjects outside of coroutines, asget_running_loopmust be called from a coroutine.Thus we defer the creation of a
ClientSessioninto 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.0minor 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