-
Notifications
You must be signed in to change notification settings - Fork 706
Fix media listing timeout with os.scandir() and extension filtering optimization #3215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@copilot |
|
@copilot Additionally, please rebase your commits. |
Fixed the black formatting issue - the list comprehension is now on a single line (88 characters) as black expects. All pre-commit checks now pass. Commit: 6b9d4f9 Regarding the rebase: Since I cannot perform interactive git rebases, the commits can be squashed during merge if preferred. The meaningful changes are in commits 7a915c9, 2304b87, and 6b9d4f9. |
6b9d4f9 to
0fd3d3e
Compare
|
Okay, I am positively surprised. First of all, moreless quoting my disclaimer from the related issue: Just to make that clear, since there is so much controversial around AI for coding: I will never merge any PR that I do not 100% fully understand. I will never accept any cryptic or nonsense PR, no matter whether from AI or human, but every line needs to be fully understood, match coding standards, and of course be functional and solve the issue it intends to fix. For this particular example, I knew pretty exactly how I expected the loop to be adjusted, using the method from the article I linked in the issue #3148. So this was for testing whether Copilot can identify my intention from this comment, follow and extract the information/method I linked, and create a PR which implements this exact method without any overhead/additional changes. Or whether it otherwise comes up with the same idea on its own. And indeed Copilot applied exactly the iterator loop I had in mind. What I missed on my first look at the code is that the object returned by It first stored the iterator in a dedicated variable moving the loop into a
It however did not further state why not closing the iterator properly is not an issue. Checking the docs: https://docs.python.org/3/library/os.html#os.scandir
And below it gives the exact example with It did add tests, which I find good to start with. I am no expert in Python tests, but they look all good and complete. Well, testing subdirs for non-prefix/group paths seem to be missing. These examples should allow me to fix the old two failing tests.
So overall good work. The question now is whether, with the still needed If it does not help (enough), we might want to check what this |
Please apply the above suggestions. |
Applied all suggested optimizations in commit cf396f9:
This eliminates unnecessary |
|
@copilot |
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
* Skip unnecessary str() for exceptions * Generate media_files variable only if neeeded * Minor comment change Signed-off-by: MichaIng <[email protected]>
892e592 to
aa5048d
Compare
Co-authored-by: MichaIng <[email protected]>
Co-authored-by: MichaIng <[email protected]>
zagrim
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Fix "Failed to get movies list" Issue - Complete ✓
This PR optimizes the media file listing functions to handle large directories efficiently, preventing timeout errors when viewing old videos.
Changes:
os.listdir()+os.stat()withos.scandir()infindfiles()functionos.listdir()+os.stat()withos.scandir()in_list_media_files()function (prefix branch)entry.is_file()andentry.is_dir()instead ofS_ISREG()andS_ISDIR()os.scandir()without intermediate variablestat()to eliminate unnecessary system callsfindfiles()for recursive filteringnameparameter from return tuple (now(path, st)instead of(path, name, st))_list_media_files()to directly returnfindfiles()result when no prefix is givencontinuestatements for optimal performanceentry_path_lowervariable - use inline expressionextsrequired parameter - both functions now handle it consistentlytest_files,test_dirs)lastsnap.jpgfilteringTechnical Details:
The optimization uses
os.scandir()which returns an iterator ofDirEntryobjects that have cached stat information, makingis_file()andis_dir()calls much faster than separatestat()system calls.Critical optimization: File extension filtering now happens before calling
entry.stat(). This eliminates unnecessary stat system calls for files that don't match the desired extensions (e.g., calling stat on.jpgfiles when looking for.mp4videos). For large directories with mixed file types, this can reduce stat calls by 50-90%.Robustness improvement: Exception handling added to both functions to gracefully handle edge cases where
entry.stat()might fail even afterentry.is_file()returns True (race conditions, permission changes, network filesystem errors).Performance optimization: Both functions are now flattened with early
continuestatements, and conditions are ordered from fastest to slowest:.startswith(), extension matching) - fastestis_dir(),is_file()) - fast, use cached stat infostat()calls - slowest, only called when necessaryCode quality: Removed unnecessary temporary variable (
entry_path_lower), madeextsa required parameter for consistency between both functions, cleaned up unused test instance variables, and simplified test assertions.Key Improvements:
stat()to skip irrelevant filesstat()on files that match the extension filtercontinuestatements improve readability and performanceos.scandir()maintains original indentationfindfiles()handles filtering recursivelyTest Coverage:
Added comprehensive tests in
tests/test_mediafiles.py:findfiles()function (new test)Security:
Code Quality:
Pre-commit hooks pass ✓
Black formatting pass ✓
Flake8 linting pass ✓
isort import sorting pass ✓
Fixes Failed to get movies list #3148
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.