[英]Load Nodejs Module into A Web Worker
I'm intending to use web worker inside my Node.js application for some concurrent tasks. 我打算在我的Node.js应用程序中使用web worker来完成一些并发任务。 However since the 'webworker-threads'
module follows the implementation of HTML5 web worker, requiring Nodejs modules like require("fs")
inside web worker does not work. 但是,由于'webworker-threads'
模块遵循HTML5 web worker的实现,因此在web worker中需要Nodejs模块(如require("fs")
不起作用。 importScripts()
can load js files but I would like a functionality inside the web worker so that I can require
npm-installed modules. importScripts()
可以加载js文件,但我想在web worker中使用一个功能,这样我就可以require
npm安装的模块。 Is there a workaround for that? 有解决方法吗?
author of webworker-threads here. 这里是webworker-threads的作者。 Thank you for using the module! 感谢您使用该模块!
There is a default native_fs_
object with the readFileSync
you can use to read files. 有一个默认的native_fs_
对象,其中包含可用于读取文件的readFileSync
。
Beyond that, I've mostly relied on onejs to compile all required modules in package.json
into a single JS file for importScripts
to use, just like one would do when deploying to a client-side web worker environment. 除此之外,我主要依靠onejs将package.json
所有必需模块编译成单个JS文件以供importScripts
使用,就像部署到客户端Web工作环境时那样。 (There are also many alternatives to onejs -- browserify, etc.) (onejs还有很多替代方案 - browserify等)
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.