简体   繁体   English

限制python脚本的RAM和CPU使用

[英]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. 我在具有大量RAM和CPU内核的Linux服务器上运行了几个python脚本。 How could I specify a maximum number of RAM and a maximum number of CPU cores for each of them ? 如何为每个RAM指定最大RAM数量和最大CPU内核数量? I could use linux bash scripts or python code to achieve this. 我可以使用linux bash脚本或python代码来实现这一目标。

You can use Linux command nice to choose the priority that you want on your process. 您可以使用Linux命令nice选择进程中所需的优先级。

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. -20最有利于该过程,而19最不利于该过程。

This will avoid to have a process consuming all your CPU when other process need it. 这样可以避免有一个进程在其他进程需要它时消耗所有CPU。

For the RAM you can use the command ulimit which allow to limit resources. 对于RAM,您可以使用命令ulimit来限制资源。

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 . 您可以使用-a选项检查外壳中的实际限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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