标签:contain 一个 event div wrap lines info https 技术
web-worker是一个兼容浏览器以及node 的web worker 实现,node 的实现基于worker_threads
yarn init -y
yarn add web-worker
const Worker = require("web-worker")
const worker = new Worker("./worker2.js");
worker.addEventListener(‘message‘, e => {
console.log(e.data) // "200 OK"
});
worker.postMessage(‘https://httpstat.us/200‘);
const fetch = require("isomorphic-fetch")
addEventListener(‘message‘, async e => {
const url = e.data;
const res = await fetch(url)
const text = await res.text();
postMessage(text);
});
node main2.js
注意node 版本需要12.8+,利用web-worker,我们可以更好的利用cpu资源,以前也有介绍过一个nodejs 的线程池实现poolifier底层实现技术也是一样的
https://github.com/developit/web-worker
https://github.com/pioardi/poolifier
web-worker 可运行在浏览器以及node 的web worker
标签:contain 一个 event div wrap lines info https 技术
原文地址:https://www.cnblogs.com/rongfengliang/p/12774383.html