Skip to content
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

Option to run process with a nice level (Linux), or lower priority (Windows) #2304

Open
wants to merge 7 commits into
base: nightly
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Update pillow requirement to 11.0.0
Update psutil requirement to 6.1.0
Update setuptools requirement to 75.3.0
Add `pywin32==308` (only for Windows, i.e. `sys_platform=='win32'`)

# New Features
Added the `character` search option to the `imdb_search` builder
Added the `-ni/--nice` (`KOMETA_NICE=true`) command line argument option to run the process at a lower priority

# Defaults
Fixed incorrect content rating mappings in various Default files
Expand Down
20 changes: 20 additions & 0 deletions docs/kometa/environmental.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,26 @@ different ways to specify these things.
docker run -it -v "X:\Media\Kometa\config:/config:rw" kometateam/kometa --width 150
```

??? blank "Nice&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`-ni`/`--nice`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`KOMETA_NICE`<a class="headerlink" href="#nice" title="Permanent link">¶</a>"

<div id="nice" />Run the Kometa process at a lower priority. Will default to normal priority if not specified.

<hr style="margin: 0px;">

**Shell Flags:** `-ni` or `--nice` (ex. `--nice`)

**Environment Variable:** `KOMETA_NICE` (ex. `KOMETA_NICE=true`)

!!! example
=== "Local Environment"
```
python kometa.py --nice
```
=== "Docker Environment"
```
docker run -it -v "X:\Media\Kometa\config:/config:rw" kometateam/kometa --nice
```

??? blank "Config Secrets&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`--kometa-***`&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`KOMETA_***`<a class="headerlink" href="#kometa-vars" title="Permanent link">¶</a>"

<div id="kometa-vars" />All Run Commands that are in the format `--kometa-***` and Environment Variables that are in the
Expand Down
27 changes: 27 additions & 0 deletions kometa.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"read-only-config": {"args": "ro", "type": "bool", "help": "Run without writing to the config"},
"divider": {"args": "d", "type": "str", "default": "=", "help": "Character that divides the sections (Default: '=')"},
"width": {"args": "w", "type": "int", "default": 100, "help": "Screen Width (Default: 100)"},
"nice": {"args": "ni", "type": "bool", "help": "Run with lower priority"},
}

parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -257,6 +258,31 @@ def new_send(*send_args, **kwargs):
plexapi.BASE_HEADERS["X-Plex-Client-Identifier"] = str(uuid_num)
ImageFile.LOAD_TRUNCATED_IMAGES = True

def low_priority():
""" Set the priority of the process to below-normal."""
try:
sys.getwindowsversion()
except AttributeError:
isWindows = False
else:
isWindows = True

if isWindows:
import win32api, win32process
# priorityclasses:
# win32process.IDLE_PRIORITY_CLASS,
# win32process.BELOW_NORMAL_PRIORITY_CLASS,
# win32process.NORMAL_PRIORITY_CLASS,
# win32process.ABOVE_NORMAL_PRIORITY_CLASS,
# win32process.HIGH_PRIORITY_CLASS,
# win32process.REALTIME_PRIORITY_CLASS
win32process.SetPriorityClass(win32api.GetCurrentProcess(), win32process.BELOW_NORMAL_PRIORITY_CLASS)
else:
os.nice(10)

if run_args["nice"]:
low_priority()

def process(attrs):
with ProcessPoolExecutor(max_workers=1) as executor:
executor.submit(start, *[attrs])
Expand Down Expand Up @@ -284,6 +310,7 @@ def start(attrs):
logger.info(f" Platform: {platform.platform()}")
logger.info(f" Total Memory: {round(psutil.virtual_memory().total / (1024.0 ** 3))} GB")
logger.info(f" Available Memory: {round(psutil.virtual_memory().available / (1024.0 ** 3))} GB")
logger.info(f" Process Priority: {'low' if run_args["nice"] else 'normal'}")
if not is_docker and not is_linuxserver:
try:
with open(os.path.abspath(os.path.join(os.path.dirname(__file__), "requirements.txt")), "r") as file:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ tenacity==9.0.0
ruamel.yaml==0.18.6
schedule==1.2.2
setuptools==75.3.0
tmdbapis==1.2.21
tmdbapis==1.2.21
pywin32==308; sys_platform == 'win32'
Loading