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
{{ message }}
Repository navigation
Performance Issue with Extracting genres Attribute from Media #1449
For the following code snippet, I observed a significant performance discrepancy:
logging.info(F"movie count: {len(movies)}")
# test1
test1_start = time.time()
for movie in movies:
logging.debug(f"{movie.__dict__.get('genres')}")
logging.info(f"duration {time.time()-test1_start}")
# test2
test2_start = time.time()
for movie in movies:
logging.debug(f"{movie.genres}")
logging.info(f"duration {time.time()-test2_start}")
Thank you for your reply. I completely understand that every time I call the media property, it requests all fields of that media from the Plex backend again. This is indeed a very good handling logic, and I agree with it.
However, I still have some concerns about performance. The reason is that there is a significant difference between this approach and directly sending requests to PMS.
Assuming I need to get the latest metadata for all media, I compared the following:
Test1: Iterate through the medias list and call the media.reload() method for each media.
Test2: Iterate through the medias list and construct the URL using f"{baseurl}{media.__dict__.get(key)}" for each media, then initiate a network request.
Understood. You are right; based on my tests, querying more metadata significantly increases the overhead. In a single-threaded environment, directly requesting the JSON is only 16ms faster on average per media item compared to constructing an object. However, with 8 threads, the difference is negligible.
For my use case, I indeed do not need that much metadata. However, using
The biggest contributor is probably just checkFiles=False because then Plex won't scan your hard drive to check the file exists. The other include parameters should have very small impact on the speed.
I think I will change the default to checkFiles=False and have the user include it explicitly if they need to check for the file.
I think the solution of changing the default values is quite good.
(However, in my use case, I ultimately opted for the approach of concatenating the URL to send network requests and parse the JSON. Because even if there are only slight differences in a single request, when there are many items in the media library, the total time quickly expands from a few seconds to over a minute. Of course, perhaps it is completely unnecessary to be so obsessed with performance optimization. A friend of mine said that people really don't care how long a scheduled script takes to run. I think he is right; in fact, when I'm actually using it, I don't care about the runtime either. It's just that during the process of writing the script, I still feel troubled by it.)
Activity
JonnyWong16 commentedon Aug 2, 2024
See responses in #713, #1007, #1128, and #1188 regarding partial objects.
And
autoreloadconfiguration option.https://python-plexapi.readthedocs.io/en/latest/configuration.html#section-plexapi-options
sqkkyzx commentedon Aug 3, 2024
Thank you for your reply. I completely understand that every time I call the
mediaproperty, it requests all fields of that media from the Plex backend again. This is indeed a very good handling logic, and I agree with it.However, I still have some concerns about performance. The reason is that there is a significant difference between this approach and directly sending requests to PMS.
Assuming I need to get the latest metadata for all media, I compared the following:
mediaslist and call themedia.reload()method for eachmedia.mediaslist and construct the URL usingf"{baseurl}{media.__dict__.get(key)}"for eachmedia, then initiate a network request.The results are:
My test code:
JonnyWong16 commentedon Aug 3, 2024
Your direct query doesn't include all the URL parameters, so you are missing a lot of metadata.
https://github.com/pkkid/python-plexapi/blob/d88f14e271828b9b435e0c8e819cf5b465eeb7a8/plexapi/base.py#L507-L527
A more apples-to-apples comparison would be
vs.
Or
vs.
The remaining difference in performance will be the small overhead for building the
PlexObjectinstead of just returning raw xml/json.sqkkyzx commentedon Aug 3, 2024
Understood. You are right; based on my tests, querying more metadata significantly increases the overhead. In a single-threaded environment, directly requesting the JSON is only 16ms faster on average per media item compared to constructing an object. However, with 8 threads, the difference is negligible.
For my use case, I indeed do not need that much metadata. However, using
seems overly cumbersome. But my problem is resolved. Thank you.
JonnyWong16 commentedon Aug 3, 2024
The biggest contributor is probably just
checkFiles=Falsebecause then Plex won't scan your hard drive to check the file exists. The other include parameters should have very small impact on the speed.I think I will change the default to
checkFiles=Falseand have the user include it explicitly if they need to check for the file.sqkkyzx commentedon Aug 4, 2024
I think the solution of changing the default values is quite good.
(However, in my use case, I ultimately opted for the approach of concatenating the URL to send network requests and parse the JSON. Because even if there are only slight differences in a single request, when there are many items in the media library, the total time quickly expands from a few seconds to over a minute. Of course, perhaps it is completely unnecessary to be so obsessed with performance optimization. A friend of mine said that people really don't care how long a scheduled script takes to run. I think he is right; in fact, when I'm actually using it, I don't care about the runtime either. It's just that during the process of writing the script, I still feel troubled by it.)