Is there any way to "put up" with the Python script method

My scripts have several components, and only some parts should be nice -d. that is, work with low priority.

Is there a nice way to use only one Python method, or do I need to split it into multiple processes?

I use Linux if that matters.

+4
source share
1 answer

You can write a decorator that updates 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 as follows:

 @low_priority def test(): pass # Or whatever you want to do. 

Denial of responsibility:

  • Works on my machine, not sure how universal os.nice is.
  • As indicated below, whether it works or not, it depends on your os / distribution or root.
  • Nice works through the process. Behavior with multiple threads per process is probably not normal and could lead to a crash.
+6
source

All Articles