Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting Web Worker CPU Utilization?

I'm looking at writing a long running CPU intensive operation using Web Workers and it does seem to be possible to push a client's CPU to 100% with these. (See Can Web Workers utilize 100% of a multi-core CPU?)

Does anyone know of any effective ways to limit the CPU utilization of web workers?

like image 273
Brian Steadman Avatar asked Oct 21 '12 16:10

Brian Steadman


1 Answers

Indeed there is a way.

Structure your computations so they can be done by repeatedly calling a function which performs a part of the work before exiting. Just before exiting, use setTimeout to schedule a new call on the worker function in a few milliseconds. The wait time can be adjusted to use nore or less CPU time.

function doWork () {
  var timer = new Date ();

 // do n cyles of work here


 timer = new Date () - timer; // time spent working 
 setTimeout (doWork, timer);  // wait an equivalent time for 50% processor load
}
like image 77
HBP Avatar answered Nov 14 '22 23:11

HBP