简体   繁体   中英

Limit RAM and CPU use for a python script

I run several python script on a linux server with lots of RAM and CPU cores. How could I specify a maximum number of RAM and a maximum number of CPU cores for each of them ? I could use linux bash scripts or python code to achieve this.

You can use Linux command nice to choose the priority that you want on your process.

nice -n 10 python yourScript.py

Positive number gives less priority to the process.

-20 is the most favorable to the process and 19 is the least favorable to the process.

This will avoid to have a process consuming all your CPU when other process need it.

For the RAM you can use the command ulimit which allow to limit resources.

Limit the RAM to 1GB (1000000 number is in kB)
ulimit -m 1000000 && python yourScript.py

You can check actual limit in your shell with option -a .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM