Last active
April 6, 2018 19:37
-
-
Save juancarlospaco/e53a1937a72008b4e11a to your computer and use it in GitHub Desktop.
Set Smooth CPUs Priority and set Process Name. Linux only.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import os | |
import logging as log | |
from ctypes import byref, cdll, create_string_buffer | |
def set_process_name_and_cpu_priority(name): | |
"""Set process name and cpu priority. | |
>>> set_process_name_and_cpu_priority("test_test") | |
True | |
""" | |
try: | |
os.nice(19) # smooth cpu priority | |
libc = cdll.LoadLibrary("libc.so.6") # set process name | |
buff = create_string_buffer(len(name.lower().strip()) + 1) | |
buff.value = bytes(name.lower().strip().encode("utf-8")) | |
libc.prctl(15, byref(buff), 0, 0, 0) | |
except Exception: | |
return False # this may fail on windows and its normal, so be silent. | |
else: | |
log.debug("Process Name set to: {0}.".format(name)) | |
return True | |
if __name__ in '__main__': | |
log.basicConfig(level=-1) # basic logger | |
set_process_name_and_cpu_priority("your_app_name_here") | |
__import__("doctest").testmod(verbose=True, report=True, exclude_empty=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of
python file.py
on the Process List will appear asyour_app_name_here
Note:
libc.so.6
is NOT a file path, is a Lib Name.try: ... except:
is needed because the code block is expected to fail on MS Window, and thats Ok.