在做多线程上传时,遇到了子线程无法创建的问题
查阅了参考资料:
https://juejin.im/post/5d4387c86fb9a06b2c32708b
https://www.electron.build/configuration/configuration.html#overridable-per-platform-options
https://www.electron.build/configuration/contents#extraresources
最终解决方法:
-
将子线程代码单独放在一个js文件中:
worker.js
项目文件结构如下: |-main.js |-source |-upload.js(主线程代码) |-worker.js(子线程代码,工作线程代码)
-
配置
electron-builder
,关键配置项如下:"build": { "asarUnpack": [ "**/node_modules/**/*" ], "extraResources": [ { "from": "source/worker.js", "to": "app.asar.unpacked/source/worker.js" } ] }
-
在代码中的引入路径的代码修改如下
const isDev = process.argv[process.argv.length - 1] === 'dev'; const worker_js_path = isDev ? path.join(__dirname, 'worker.js') : path.join(process.resourcesPath, 'app.asar.unpacked/source/worker.js'); const worker = new Worker(worker_js_path);
版权声明:本文为u014705309原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。