Python: ulimit and nice for subprocess.call / subprocess.Popen?

后端 未结 3 995
Happy的楠姐
Happy的楠姐 2020-12-12 14:03

I need to limit the amount of time and cpu taken by external command line apps I spawn from a python process using subprocess.call , mainly because sometimes the spawned pro

3条回答
  •  误落风尘
    2020-12-12 14:26

    You can set limits for subprocesses with the ulimit and nice shell commands like this:

    import subprocess
    subprocess.Popen('ulimit -t 60; nice -n 15 cpuhog', shell=True)
    

    This runs cpuhog with a limit of 60 seconds of CPU time and a niceness adjustment of 15. Note that there is no simple way to set a 20% CPU throttle as such. The process will use 100% CPU unless another (less nice) process also needs the CPU.

提交回复
热议问题