我正在考虑使用 Web Workers 编写一个长时间运行的 CPU 密集型操作,并且似乎可以使用这些将客户端的 CPU 推到 100%。(请参阅Web Workers 能否利用 100% 的多核 CPU?)
有谁知道任何有效的方法来限制网络工作者的 CPU 利用率?
我正在考虑使用 Web Workers 编写一个长时间运行的 CPU 密集型操作,并且似乎可以使用这些将客户端的 CPU 推到 100%。(请参阅Web Workers 能否利用 100% 的多核 CPU?)
有谁知道任何有效的方法来限制网络工作者的 CPU 利用率?
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
}