Skip to content

Commit c340c3f

Browse files
committed
Avoid explicit calls to select.select()
Use higher level `selectors` module instead: https://docs.python.org/3/library/selectors.html. Selectors uses the most efficient implementation available on the current platform. On Linux, it defaults to using: ``` $ python3 -c "import selectors; print(selectors.DefaultSelector())" <selectors.EpollSelector object at 0x7a6a66f02120> ```
1 parent e54f96c commit c340c3f

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

exiftool/exiftool.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"""
3131

3232
# ---------- standard Python imports ----------
33-
import select
33+
import selectors
3434
import subprocess
3535
import os
3636
import shutil
@@ -114,6 +114,10 @@ def _read_fd_endswith(fd, b_endswith: bytes, block_size: int) -> bytes:
114114
115115
if you're not careful, on windows, this will block
116116
"""
117+
if not constants.PLATFORM_WINDOWS:
118+
selector = selectors.DefaultSelector()
119+
selector.register(fd, selectors.EVENT_READ)
120+
117121
output_list: List[bytes] = []
118122

119123
# if we're only looking at the last few bytes, make it meaningful. 4 is max size of \r\n? (or 2)
@@ -129,9 +133,9 @@ def _read_fd_endswith(fd, b_endswith: bytes, block_size: int) -> bytes:
129133
output_list.append(os.read(fd, block_size))
130134
else: # pytest-cov:windows: no cover
131135
# this does NOT work on windows... and it may not work on other systems... in that case, put more things to use the original code above
132-
inputready, outputready, exceptready = select.select([fd], [], [])
133-
for i in inputready:
134-
if i == fd:
136+
events = selector.select()
137+
for key, _ in events:
138+
if key.fd == fd:
135139
output_list.append(os.read(fd, block_size))
136140

137141
return b"".join(output_list)

0 commit comments

Comments
 (0)