Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 322
Nerian Avatar asked May 15 '11 14:05

Nerian


1 Answers

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.

like image 124
mu is too short Avatar answered Oct 13 '22 06:10

mu is too short