开发者

possible to force, a script to use certain amount of CPU and memory?

is it possible to force a ruby script to use up to certain amount of CPU and memory.

i dont want the script to be killed when it exceeds this specified amount. i just want it to run within the given constraints.

EDIT:

yes its an endless recursive loop that seems to use lot of开发者_StackOverflow CPU.

i noticed that doing return at the end of each recursion is causing this. after i remove it, this high cpu usage is gone. what else can i use to terminate the loop ? exit ?


Yes, calling a sleep function in most programming systems (ruby included) will cause the program to wait for that amount of time, using little to no CPU power.

Alternatively, you could run your program at a lower priority (in *nix systems, this is done with nice or renice).


sleep will sleep the current thread for some period of time. Your cpu load goes down because your programme isn't doing anything for that time. The kernel should handle ensuring that your CPU has sufficient time for all the programmes running.


It really depends what you do in your script. If it's some sort of a endless loop, you are just "hibernating" your script, and allow less processing time to be spent on it.

In short, "sleeping" is not a particularly clean or proper solution. It would help if you posted details on exactly what your script does, typically there would a much more sensible solution available.


You should almost never need to do this. Why would you waste that time doing nothing? There's nothing wrong with the CPU being at high utilisation; it doesn't need to rest. If there are multiple processes running then the operating system deals with dividing the CPU time between them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜