SUCHMOKUO / node-worker-threads-pool

Simple worker threads pool using node's worker_threads module.
MIT License
289 stars 29 forks source link

Static classes cannot be used #52

Open YongKJ opened 2 years ago

YongKJ commented 2 years ago

I tried to export Excel data using a static utility class, but something happened.

the code: `

 public test14(): void {
    let lstHeader = [
        ["a", "a", "e"],
        ["b", "b", "e"],
        ["b", "b", "f"],
        ["c", "a", "a"],
        ["c", "a", "a"]
    ];

    let pool = new StaticPool({
        size: 10,
        workerData: ExcelUtil,
        task: num => {
            //write header data
            ExcelUtil.writeHeader(lstHeader, 22);
            for (let i = 0, rowIndex = 3; i < num * 50 + num; i++, rowIndex += 2) {
                for (let j = 0; j < 5; j++) {
                    //write cell data
                    ExcelUtil.writeCellData(rowIndex, j, "test");
                    ExcelUtil.writeCellData(rowIndex + 1, j, "demo");
                }
            }
            //get data sheet
            let sheet = ExcelUtil.getDataSheet();
            return {sheet: sheet, name: "Sheet" + num};
        }
    });

    let works: Array<Promise<void>> = [];
    let lstData: Array<Record<string, any>> = [];
    for (let i = 1; i <= 5; i++) {
        works.push(new Promise<void>(resolve => {
            pool.exec(i).then(result => {
                lstData.push(result);
                resolve();
            });
        }));
    }

    Promise.all(works).then(() => {
        pool.destroy().then();
        LogUtil.loggerLine(Log.of("ApplicationTest", "test14", "lstData", lstData));
        ExcelUtil.write(lstData, "C:\\Users\\YongKJ\\Desktop\\demo-" + Date.now() + ".xlsx");
    });

}

`

The error: `

    internal/worker.js:204
        this[kPort].postMessage({
                    ^
    DOMException [DataCloneError]: class ExcelUtil {
        //获取excel表数据
        static toMap(excelName, sheetName, headerRow, headerCol, headerLastCol...<omitted>...
    } could not be cloned.

`

oneEyedSunday commented 3 months ago

Seems to support only strings and basic types as inputs, or you could use transferlist https://www.npmjs.com/package/node-worker-threads-pool#statictaskexecutorsettransferlisttransferlist