如何从 angular 应用程序的主线程中终止一组网络工作者?
How to terminate set of web workers from the main thread in an angular app?
我正在开发一个 angular 应用程序,它创建多个网络工作者并在工作线程中执行单独的工作。我的应用程序通过单击按钮读取输入的文件,然后根据文件中给定的 [array.length] 创建新的 Workers。它运作良好。现在,我需要做的是通过单击停止按钮来终止所有网络工作者。下面是我在两次按钮点击中使用的两种方法。
public i = 0;
public file: any;
public noOfOrders: number;
private worker: Worker;
public uploadDocument(): void {
const fileReader = new FileReader();
fileReader.onload = (e) => {
const k = JSON.parse(fileReader.result.toString());
this.noOfOrders = Math.round(k.orders_per_second / k.credentials.length);
if (typeof Worker !== 'undefined') {
while (this.i < k.credentials.length) {
this.worker = new Worker('./app.worker', { type: 'module' });
this.worker.onmessage = ({ data }) => {
console.log(data + k.credentials.length);
};
this.worker.postMessage({ data: k.credentials[this.i], orders: this.noOfOrders });
this.i++;
}
}
};
fileReader.readAsText(this.file);
}
public stop(): void { // this method only terminates the final worker thread
this.worker.terminate();
}
你每次都在覆盖同一个工人,这就是为什么你只能终止最后一个。
相反,将它们存储在一个工人数组中,然后对所有工人调用 terminate()
。像这样:
public i = 0;
public file: any;
public noOfOrders: number;
private workers: Worker[];
public uploadDocument(): void {
const fileReader = new FileReader();
fileReader.onload = (e) => {
const k = JSON.parse(fileReader.result.toString());
this.noOfOrders = Math.round(k.orders_per_second / k.credentials.length);
if (typeof Worker !== 'undefined') {
while (this.i < k.credentials.length) {
const worker = new Worker('./app.worker', { type: 'module' });
worker.onmessage = ({ data }) => {
console.log(data + k.credentials.length);
};
worker.postMessage({ data: k.credentials[this.i], orders: this.noOfOrders });
this.workers.push(worker);
this.i++;
}
}
};
fileReader.readAsText(this.file);
}
public stop(): void {
this.workers.forEach(w => w.terminate());
}
我正在开发一个 angular 应用程序,它创建多个网络工作者并在工作线程中执行单独的工作。我的应用程序通过单击按钮读取输入的文件,然后根据文件中给定的 [array.length] 创建新的 Workers。它运作良好。现在,我需要做的是通过单击停止按钮来终止所有网络工作者。下面是我在两次按钮点击中使用的两种方法。
public i = 0;
public file: any;
public noOfOrders: number;
private worker: Worker;
public uploadDocument(): void {
const fileReader = new FileReader();
fileReader.onload = (e) => {
const k = JSON.parse(fileReader.result.toString());
this.noOfOrders = Math.round(k.orders_per_second / k.credentials.length);
if (typeof Worker !== 'undefined') {
while (this.i < k.credentials.length) {
this.worker = new Worker('./app.worker', { type: 'module' });
this.worker.onmessage = ({ data }) => {
console.log(data + k.credentials.length);
};
this.worker.postMessage({ data: k.credentials[this.i], orders: this.noOfOrders });
this.i++;
}
}
};
fileReader.readAsText(this.file);
}
public stop(): void { // this method only terminates the final worker thread
this.worker.terminate();
}
你每次都在覆盖同一个工人,这就是为什么你只能终止最后一个。
相反,将它们存储在一个工人数组中,然后对所有工人调用 terminate()
。像这样:
public i = 0;
public file: any;
public noOfOrders: number;
private workers: Worker[];
public uploadDocument(): void {
const fileReader = new FileReader();
fileReader.onload = (e) => {
const k = JSON.parse(fileReader.result.toString());
this.noOfOrders = Math.round(k.orders_per_second / k.credentials.length);
if (typeof Worker !== 'undefined') {
while (this.i < k.credentials.length) {
const worker = new Worker('./app.worker', { type: 'module' });
worker.onmessage = ({ data }) => {
console.log(data + k.credentials.length);
};
worker.postMessage({ data: k.credentials[this.i], orders: this.noOfOrders });
this.workers.push(worker);
this.i++;
}
}
};
fileReader.readAsText(this.file);
}
public stop(): void {
this.workers.forEach(w => w.terminate());
}