You could write a decorator that renices the running process on entry and exit:
import os
import functools
def low_priority(f):
@functools.wraps(f)
def reniced(*args, **kwargs):
os.nice(5)
try:
f(*args,**kwargs)
finally:
os.nice(-5)
return reniced
Then you can use it this way:
@low_priority
def test():
pass # Or whatever you want to do.
Disclaimers:
- Works on my machine, not sure how universal os.nice is.
- As noted below, whether it works or not may depend on your os/distribution, or on being root.
- Nice is on a per-process basis. Behaviour with multiple threads per process will likely not be sane, and may crash.