[GH-ISSUE #159] Sessions and Persistent TCP connections #119

Closed
opened 2026-02-27 22:08:08 +03:00 by kerem · 6 comments
Owner

Originally created by @0xbrayo on GitHub (Jan 26, 2021).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/159

Hi, I'm using this module to fetch data about multiple songs at a go, I noticed that the script creates a new TCP connection for each request, which adds about 2 to 3 RTTs to each song requests, anyway, I would appreciate it if you would provide for the user the capability to pass a requests.session() to the YTMusicClient. Or find a way to keep a master session within the class for each new class. I would be down to help you with implementing it if it's a feature you will deem necessary

Originally created by @0xbrayo on GitHub (Jan 26, 2021). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/159 Hi, I'm using this module to fetch data about multiple songs at a go, I noticed that the script creates a new TCP connection for each request, which adds about 2 to 3 RTTs to each song requests, anyway, I would appreciate it if you would provide for the user the capability to pass a requests.session() to the YTMusicClient. Or find a way to keep a master session within the class for each new class. I would be down to help you with implementing it if it's a feature you will deem necessary
kerem 2026-02-27 22:08:08 +03:00
Author
Owner

@sigma67 commented on GitHub (Jan 26, 2021):

Sounds like a good idea. We could initiate the session on instantiation and reuse it in _send_request. We can use the tests to measure before-after performance.

I'd prefer to make it the default behavior if performance results are good. Feel free to make some suggestions of your own or start implementing if you'd like.

Edit: We can probably take inspiration from spotipy: https://github.com/plamere/spotipy/blob/master/spotipy/client.py#L191

<!-- gh-comment-id:767424037 --> @sigma67 commented on GitHub (Jan 26, 2021): Sounds like a good idea. We could initiate the session on instantiation and reuse it in _send_request. We can use the tests to measure before-after performance. I'd prefer to make it the default behavior if performance results are good. Feel free to make some suggestions of your own or start implementing if you'd like. Edit: We can probably take inspiration from spotipy: https://github.com/plamere/spotipy/blob/master/spotipy/client.py#L191
Author
Owner

@0xbrayo commented on GitHub (Jan 26, 2021):

That's exactly where I got the idea from

<!-- gh-comment-id:767430014 --> @0xbrayo commented on GitHub (Jan 26, 2021): That's exactly where I got the idea from
Author
Owner

@sigma67 commented on GitHub (Jan 27, 2021):

Preliminary results are looking good, shaves off about 0.3s or 6% off the best case total test time when running test_search. Obviously many factors at play here, but it should be a great improvement for users with many calls per instance.

<!-- gh-comment-id:768304838 --> @sigma67 commented on GitHub (Jan 27, 2021): Preliminary results are looking good, shaves off about 0.3s or 6% off the best case total test time when running `test_search`. Obviously many factors at play here, but it should be a great improvement for users with many calls per instance.
Author
Owner

@sigma67 commented on GitHub (Jan 29, 2021):

Do you think the current implementation is satisfactory to close this issue @brayo-pip ?

<!-- gh-comment-id:769695046 --> @sigma67 commented on GitHub (Jan 29, 2021): Do you think the current implementation is satisfactory to close this issue @brayo-pip ?
Author
Owner

@0xbrayo commented on GitHub (Jan 29, 2021):

I was offline, sorry lemme review and ger back to you

<!-- gh-comment-id:770077945 --> @0xbrayo commented on GitHub (Jan 29, 2021): I was offline, sorry lemme review and ger back to you
Author
Owner

@0xbrayo commented on GitHub (Jan 29, 2021):

I think it's perfect, I like that there's the option to either pass a Session or simply use an in-class Session

<!-- gh-comment-id:770080276 --> @0xbrayo commented on GitHub (Jan 29, 2021): I think it's perfect, I like that there's the option to either pass a Session or simply use an in-class Session
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ytmusicapi#119
No description provided.