How could a Ruby process put limit to its CPU usage?
Let say I want a Ruby process to not use more than 15% 开发者_运维知识库of the CPU. Is it possible? How?
You could try using Process.setrlimit
from the standard core:
Sets the resource limit of the process.
This looks like it is just a wrapper around the setrlimit
from the C library so it may only be available on Unix-ish platforms. setrlimit
doesn't support CPU percentage limits but it does support limiting CPU time in seconds.
If you're just trying to keep your Ruby process from hogging the whole CPU then you could try adjusting its priority with Process.setpriority
which is just a wrapper around libc's setpriority
and offers some control over your process's scheduling priority. Again, availability will probably be limited by your platform but it should work on Linux, OSX, or any other Unix-ish system.
精彩评论