Skip to content

Commit c485fe4

Browse files
committed
Fix io priority handling
io priority was set with process priority values instead of io priority values. Also, setting process/io priority now shows proper access denied when not allowed to. Also, allow case insensitive priority settings.
1 parent ccc2978 commit c485fe4

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,10 @@ exit_code, output = command_runner('ping 127.0.0.1', on_exit=do_something)
404404
```
405405

406406
### Process and IO priority
407-
`command_runner` can set it's subprocess priority to 'low', 'medium' or 'high', which translate to 15, 0, -15 niceness on Linux and BELOW_NORMAL_PRIORITY_CLASS and HIGH_PRIORITY_CLASS in Windows.
407+
`command_runner` can set it's subprocess priority to 'low', 'normal' or 'high', which translate to 15, 0, -15 niceness on Linux and BELOW_NORMAL_PRIORITY_CLASS and HIGH_PRIORITY_CLASS in Windows.
408+
On Linux, you may also directly use priority with niceness int values.
408409

409-
The same applies to IO bound priority.
410+
You may also set subprocess io priority to 'low', 'normal' or 'high'.
410411

411412
Example:
412413
```python

command_runner/__init__.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,19 @@ def _set_priority(
221221
Set process and / or io priorities
222222
Since Windows and Linux use different possible values, let's simplify things by allowing 3 prioriy types
223223
"""
224+
priority = priority.lower()
225+
224226
if priority_type == "process":
225227
if isinstance(priority, int) and os.name != "nt" and -20 <= priority <= 20:
226228
raise ValueError("Bogus process priority int given: {}".format(priority))
227-
elif priority.lower() not in ["low", "normal", "high"]:
229+
elif priority not in ["low", "normal", "high"]:
228230
raise ValueError(
229231
"Bogus {} priority given: {}".format(priority_type, priority)
230232
)
231233

234+
if priority_type == "io" and priority not in ["low", "normal", "high"]:
235+
raise ValueError("Bogus {} priority given: {}".format(priority_type, priority))
236+
232237
if os.name == "nt":
233238
priorities = {
234239
"process": {
@@ -901,25 +906,37 @@ def _monitor_process(
901906
# Set process priority if given
902907
if priority:
903908
try:
904-
set_priority(process.pid, priority)
909+
try:
910+
set_priority(process.pid, priority)
911+
except psutil.AccessDenied as exc:
912+
logger.warning(
913+
"Cannot set process priority {}. Access denied.".format(exc)
914+
)
915+
except Exception as exc:
916+
logger.warning("Cannot set process priority: {}".format(exc))
917+
logger.debug("Trace:", exc_info=True)
905918
except NameError:
906919
logger.warning(
907920
"Cannot set process priority. No psutil module installed."
908921
)
909922
logger.debug("Trace:", exc_info=True)
910-
except Exception as exc:
911-
logger.warning("Cannot set process priority: {}".format(exc))
912-
logger.debug("Trace:", exc_info=True)
913-
914-
# Set ioi priority if given
923+
# Set io priority if given
915924
if io_priority:
916925
try:
917-
set_io_priority(process.pid, priority)
926+
try:
927+
set_io_priority(process.pid, io_priority)
928+
except psutil.AccessDenied as exc:
929+
logger.warning(
930+
"Cannot set io priority for process {}: access denied.".format(
931+
exc
932+
)
933+
)
934+
except Exception as exc:
935+
logger.warning("Cannot set io priority: {}".format(exc))
936+
logger.debug("Trace:", exc_info=True)
937+
raise
918938
except NameError:
919939
logger.warning("Cannot set io priority. No psutil module installed.")
920-
except Exception as exc:
921-
logger.warning("Cannot set io priority: {}".format(exc))
922-
logger.debug("Trace:", exc_info=True)
923940

924941
try:
925942
# let's return process information if callback was given

0 commit comments

Comments
 (0)