Nerian
Nerian

Reputation: 16197

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?

Upvotes: 8

Views: 2196

Answers (1)

mu is too short
mu is too short

Reputation: 434755

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.

Upvotes: 11

Related Questions