Making a process max out at a certain percentage of CPU is not easy to do (because it's generally not a useful thing to do; if the CPU isn't otherwise used, why not use 100%? and if there are other things running, setting different priorities works out better) especially I think on Windows. It may be sufficient for your purposes just to put in a line like sleep 1 unless $. % 100 which makes perl stop for at least a second every 100 lines.while (my $logline = <LOG>) { # parse it }
However, you may be able to get the linux code you posted to work by getting rid of sub Cat entirely and making CPU_used like:
But unless I'm totally misunderstanding it, you should be using CPU_max(.2) to get a max of 20% CPU use.sub CPU_used { (map $_->[0]+$_->[1], [times])[0] }
Update: note that calling CPU_max every iteration is counterproductive; I'd figure out how many lines you process per second and make your code:
(using your lines per second figure, and adjusting if needed for finer granularity.)CPU_start(); while (my $logline = <LOG>) { # parse it CPU_max(.2) unless $. % $lines_per_second; }
In reply to Re: CPU utilization
by ysth
in thread CPU utilization
by contact_chatan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |