Skip to content

Commit e41a950

Browse files
committed
新增多线程错误捕获到主线程
1 parent 951ef85 commit e41a950

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

Workers/AudioProcesserWorker.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,21 @@ async function main() {
3434
const { MyWorkerScript, AudioContainer, AudioData, StftData, Float32Matrix } = await init_import();
3535
const pinyin = await prepare_pinyin();
3636
const myWorkerScript = new MyWorkerScript(self);
37-
function log(msg) {
38-
// console.log(`[MyWorkerScript]${msg}`);
39-
myWorkerScript.sendData('Log', msg);
40-
};
4137
myWorkerScript.reciveData('initInfo',
4238
async (dataContent) => {
43-
log(`收到initInfo,准备初始化audioContainer`);
39+
myWorkerScript.log(`收到initInfo,准备初始化audioContainer`);
4440
self.audioContainer = new AudioContainer(
4541
dataContent.sampleRate,
4642
dataContent.fft_s,
4743
dataContent.hop_s,
4844
dataContent.numberOfChannels,
4945
dataContent.max_duration,
5046
false);
51-
log(`准备加载model...`);
47+
myWorkerScript.log(`准备加载model...`);
5248
self.model = await init_model();
5349
const len = Math.round(self.audioContainer.max_duration / self.audioContainer.hop_s);
5450
self.model.predict(tf.zeros([1, len, 129]));
55-
log(`当前tensorflowJS的Backend:${tf.getBackend()}`);
51+
myWorkerScript.log(`当前tensorflowJS的Backend:${tf.getBackend()}`);
5652
myWorkerScript.sendData('Event', 'inited');
5753
}
5854
);

Workers/MyWorker.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@ class MyWorker {
77
if (this.reciveDataFunctions[data.type] instanceof Function) {
88
this.reciveDataFunctions[data.type](data.content);
99
} else {
10-
console.error(`收到的data.type:${data.type}没有注册处理函数!`);
10+
console.error(`[MyWorker]收到的data.type:${data.type}没有注册处理函数!`);
1111
console.log(data);
1212
};
1313
};
14+
this.Worker.onerror = function (err) {
15+
console.log('[MyWorker]worker is suffering!', err)
16+
};
1417
this.reciveDataFunctions = {};
18+
this.reciveData('Log', (msg) => { console.log(`[MyWorkerScript]${msg}`); });
19+
this.reciveData('Error', (error_args) => {
20+
console.error(`[MyWorkerScript]遇到错误!\n${error_args}`);
21+
});
1522
this.reciveData('UnknownDatatype', (data) => {
16-
console.error(`[MainThread]发送给${this.WebWorkScriptURL}的data.type为:${data.type}的数据其MyWorkerScript没有注册处理函数!`);
23+
console.error(`[MyWorker]发送给${this.WebWorkScriptURL}的data.type为:${data.type}的数据其MyWorkerScript没有注册处理函数!`);
1724
console.log(data);
18-
})
25+
});
1926
};
2027

2128
sendData = (dataType, dataContent, transferList = []) => {
@@ -26,23 +33,43 @@ class MyWorker {
2633
};
2734

2835
reciveData = (dataType, dealDataContent = (dataContent) => { }) => {
36+
if (this.reciveDataFunctions[dataType]) console.warn(`警告!dataType:${dataType}的处理函数已经存在,进行覆盖!`)
2937
this.reciveDataFunctions[dataType] = dealDataContent;
3038
};
39+
40+
unreciveData = (dataType) => {
41+
if (dataType in this.reciveDataFunctions) delete this.reciveDataFunctions[dataType];
42+
};
3143
};
3244

3345
class MyWorkerScript {
3446
constructor(worker_self) {
35-
this.reciveDataFunctions = {};
3647
this.worker_self = worker_self;
37-
worker_self.onmessage = ({ data }) => {
48+
this.reciveDataFunctions = {};
49+
50+
this.worker_self.onmessage = ({ data }) => {
3851
if (this.reciveDataFunctions[data.type] instanceof Function) {
3952
this.reciveDataFunctions[data.type](data.content);
4053
} else {
41-
console.error(`[WorkerThread]收到的data的data.type:${data.type}没有注册处理函数!`);
54+
console.error(`[MyWorkerScript]收到的data的data.type:${data.type}没有注册处理函数!`);
4255
console.log(data);
4356
this.sendData('UnknownDatatype', data);
4457
};
4558
};
59+
this.worker_self.onerror = (...args) => {
60+
this.sendData('Error', args);
61+
};
62+
63+
this.worker_self.addEventListener('unhandledrejection', (event) => {
64+
// the event object has two special properties:
65+
// event.promise - the promise that generated the error
66+
// event.reason - the unhandled error object
67+
this.sendData('Error', event.reason);
68+
});
69+
};
70+
71+
log = (msg) => {
72+
this.sendData('Log', msg);
4673
};
4774

4875
sendData = (dataType, dataContent, transferList = [], targetOrigin = undefined) => {

main.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ switch_btn.onclick = async function (e) {
8484
const sampleRate = 8000, fft_s = 0.032, hop_s = 0.008;
8585
const audioContainer = new AudioContainer(sampleRate, fft_s, hop_s, 1, 10);
8686
const myWorker = new MyWorker('./Workers/AudioProcesserWorker.js');
87-
myWorker.reciveData('Log', (msg) => { console.log(`[MyWorkerScript]${msg}`); });
88-
// myWorker.reciveData('predict_res', (content) => { console.log(`输出长度:${content.length},${content[0].length},${content[0][0].length}`) });
8987
myWorker.reciveData('pinyinArray', (pinyinArray) => { console.log(pinyinArray) });
9088
myWorker.reciveData('Event', (content) => {
9189
switch (content) {

0 commit comments

Comments
 (0)