首页 文章 精选 留言 我的

精选列表

搜索[泛型],共10000篇文章
优秀的个人博客,低调大师

Node.js 中处理 CPU 密集型任务的实用方案:从阻塞事件循环到 worker_threads

在 Node.js 服务里做图片压缩、PDF 解析或者大 JSON 序列化时,经常遇到接口响应突然变慢、健康检查超时、甚至整个进程卡住的情况。排查下来往往不是数据库慢,而是某段同步计算把事件循环堵死了。Node.js 主线程既要处理网络 I/O 又要跑 JavaScript,一旦遇到长时间同步运算,所有回调都得排队,表现就是并发能力骤降。这篇文章从实际代码出发,先复现事件循环被阻塞的现象,再讨论用 worker_threads 把计算挪到独立线程的落地方式,包括线程池管理、消息传递、错误处理和优雅退出,最后给出在 Express 服务中接入的完整示例。

优秀的个人博客,低调大师

Node.js 中处理 CPU 密集型任务的正确姿势:从阻塞事件循环到 Worker Threads 实战

在 Node.js 服务里跑图片压缩、PDF 解析或者加解密逻辑时,经常遇到一个尴尬现象:明明接口已经做了异步封装,但某个请求一进来,整个进程的其他请求全卡住,健康检查探针超时,日志也停止输出。根本原因是这些计算任务运行在主线程上,把事件循环堵死了。Node.js 的单线程模型擅长 I/O 密集场景,可一旦遇到大循环、复杂正则、同步加密函数,事件循环就没机会处理其他回调。很多人第一反应是用 setImmediate 或 process.nextTick 把任务拆开,但这只是把阻塞点延后,并没有真正释放主线程。这篇文章从实际代码出发,讲清楚事件循环被阻塞的排查方式,以及如何用 worker_threads 把 CPU 任务挪到独立线程,并处理好线程池复用、错误传递和进程退出这些容易踩坑的细节。

优秀的个人博客,低调大师

算力中心温湿度记录仪,网络型 RJ45 设备接入动环平台工程实践

随着智算中心、超算集群、IDC 高密度算力机房建设规模持续扩张,单机柜功率密度不断抬升,部分高算力机柜负载突破 15kW、25kW,局部热点、风道紊流、冷热通道短路已经成为算力硬件宕机、加速卡降频、服务器寿命衰减的核心诱因。传统机房动环监控多采用少量墙面点位温湿度采集,采样稀疏,无法捕捉机柜内部、进风口、出风口、背板、交换机区域的微环境差异;而独立式无联网记录仪只能本地存储,数据孤立,无法和动环平台、楼宇自控、算力调度系统联动,一旦出现局部高温高湿,告警滞后,极易引发批量算力节点故障,造成业务中断、算力任务失败,带来高额经济损失。

优秀的个人博客,低调大师

分散式机房动环配套,RJ45 网络型温湿度探测器集中运维方案

在政企、运营商、金融以及制造业信息化基础设施建设场景中,大量机房并非集中在同一园区,而是以分散式部署形态存在:区县分支机构机房、边缘算力节点、通信接入机房、车间独立控制柜机房、楼宇弱电间等,单机房规模偏小,数量多、地理位置跨度大,运维人员无法常驻现场。传统动环监控方案大多采用单机房本地独立主机、本地存储、现场值守巡检的模式,面对分散机房集群时,运维成本高、故障发现滞后、版本不统一、设备台账混乱,一旦出现高温、凝露、局部湿度过高,极易引发服务器、交换机、UPS、蓄电池等核心设备加速老化,甚至出现宕机、短路、绝缘下降等重大风险。

优秀的个人博客,低调大师

Node.js 中使用 Worker Threads 处理 CPU 密集型任务:从 Event Loop 阻塞到线程池隔离

在 Node.js 服务里跑图像处理、加解密、大 JSON 解析或者复杂规则计算时,event loop 被长时间占用导致健康检查超时、其他请求排队,是线上很常见的问题。单线程模型下,一个同步 for 循环就能让整个进程卡住,setTimeout 回调全部延迟,HTTP 连接堆积。很多人第一反应是开 cluster 多进程,但进程间内存不共享,通信开销大,而且每个进程重复加载模块。Worker Threads 提供了更轻量的隔离方式,可以在同一进程内把 CPU 任务丢到独立线程,主线程继续响应 I/O。这篇文章从实际场景出发,讲清楚 worker_threads 的启动开销、数据传递方式、SharedArrayBuffer 的使用边界,以及如何用线程池避免频繁创建 worker 线程带来的抖动。

优秀的个人博客,低调大师

Node.js 中处理 CPU 密集型任务时,为什么你的 worker_threads 没有生效?

在 Node.js 里跑图片处理、加密解密或者大 JSON 解析时,主线程被占满导致接口响应变慢是挺常见的事。很多人知道用 worker_threads 把计算挪到子线程,但实际写完之后发现 CPU 占用还是只在一个核上,或者任务反而更慢了。问题往往出在几个细节上:传过去的数据被结构化克隆了一遍、worker 启动开销被忽略、或者误以为 worker 里能直接共享主线程的变量。下面结合一个视频封面缩放的场景,聊聊 worker_threads 真正生效需要绕开的坑,以及怎么判断它到底有没有帮上忙。 先看一个典型的错误写法。业务里需要把用户上传的图片批量生成多种尺寸的缩略图,代码直接在主线程里循环调用 sharp: ```js const sharp = require('sharp'); async function resizeAll(files) { const results = []; for (const file of files) { const buf = await sharp(file).resize(320).toBuffer(); results.push(buf); } return results; } ``` 这段代码里 resize 本身是 libvips 的 C++ 逻辑,但 `toBuffer` 的 Promise 回调、文件读取、结果拼接都在主线程的事件循环里排队。文件一多,主线程的延迟肉眼可见。改成 worker_threads 时,有人会这么写: ```js // worker.js const { parentPort, workerData } = require('worker_threads'); const sharp = require('sharp'); (async () => { const out = await sharp(workerData.file).resize(320).toBuffer(); parentPort.postMessage(out); })(); ``` ```js const { Worker } = require('worker_threads'); function resizeInWorker(file) { return new Promise((resolve, reject) => { const worker = new Worker('./worker.js', { workerData: { file } }); worker.once('message', resolve); worker.once('error', reject); }); } ``` 功能是对的,但每个文件都 new 一个 Worker 就踩了启动开销的坑。Worker 初始化要加载模块、创建 V8 isolate,单次成本在几十毫秒量级。图片只有几张时,总耗时可能比主线程直接跑还长。正确做法是维护一个固定大小的 worker 池,任务通过消息队列分发。可以用 `os.availableParallelism()` 拿到可用核数,池大小一般不超过这个值减去 1,给主线程留出处理网络 I/O 的空间。 第二个坑是数据传输。`workerData` 和 `postMessage` 默认走结构化克隆,Buffer 会被完整复制一份。图片动辄几 MB,复制成本很容易吃掉并行带来的收益。Node 提供了 `transferList` 把 ArrayBuffer 的所有权转移过去,避免拷贝: ```js const { Worker } = require('worker_threads'); function runTask(buffer) { return new Promise((resolve, reject) => { const worker = new Worker('./worker.js'); const ab = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); worker.once('message', resolve); worker.once('error', reject); worker.postMessage({ data: ab }, [ab]); }); } ``` 注意转移之后主线程这边的 ArrayBuffer 会变成 detached 状态,不能再访问。如果主线程后续还要用这份数据,就得先复制再转移,或者干脆让 worker 把结果写回文件、主线程只拿路径。 还有一个容易忽略的点是 worker 里的异常处理。worker 抛出的未捕获异常会触发 `error` 事件,但如果代码里只监听了 `message`,Promise 会一直挂起,连接池里的 worker 也可能泄漏。在池的实现里,每个任务结束或者出错后都要把 worker 放回空闲队列,或者在 `exit` 事件里补一个新的 worker 进来。 判断 worker_threads 是否真的生效,不要只看接口是不是变快了。用 `process.cpuUsage()` 在任务前后打点,看 user CPU 时间是否接近任务耗时乘以核数;再用 `perf_hooks` 的 `monitorEventLoopDelay` 观察主线程事件循环的延迟分布。如果主线程延迟峰值明显下降、CPU 时间分摊到多个核上,说明并行确实起了作用。反过来,如果 worker 池加完之后总耗时没变甚至变长,先检查是不是任务粒度太小、数据传输量太大,或者 worker 里又做了同步阻塞的 I/O。 最后提一下共享内存的场景。多个 worker 需要读同一份大配置或者做累加统计时,`SharedArrayBuffer` 配合 `Atomics` 能省掉重复复制,但同步逻辑写错很容易出现竞态。大多数业务场景下,用消息传递加合理的任务划分已经够用,不必一上来就上共享内存。 推荐查看天气:http://www.tianqi92.com/danci/3255.html 推荐查看天气:http://www.tianqi92.com/danci/3256.html 推荐查看天气:http://www.tianqi92.com/danci/3257.html 推荐查看天气:http://www.tianqi92.com/danci/3258.html 推荐查看天气:http://www.tianqi92.com/danci/3259.html 推荐查看天气:http://www.tianqi92.com/danci/3260.html 推荐查看天气:http://www.tianqi92.com/danci/3261.html 推荐查看天气:http://www.tianqi92.com/danci/3262.html 推荐查看天气:http://www.tianqi92.com/danci/3263.html 推荐查看天气:http://www.tianqi92.com/danci/3264.html 推荐查看天气:http://www.tianqi92.com/danci/3265.html 推荐查看天气:http://www.tianqi92.com/danci/3266.html 推荐查看天气:http://www.tianqi92.com/danci/3267.html 推荐查看天气:http://www.tianqi92.com/danci/3268.html 推荐查看天气:http://www.tianqi92.com/danci/3269.html 推荐查看天气:http://www.tianqi92.com/danci/3270.html 推荐查看天气:http://www.tianqi92.com/danci/3271.html 推荐查看天气:http://www.tianqi92.com/danci/3272.html 推荐查看天气:http://www.tianqi92.com/danci/3273.html 推荐查看天气:http://www.tianqi92.com/danci/3274.html 推荐查看天气:http://www.tianqi92.com/danci/3275.html 推荐查看天气:http://www.tianqi92.com/danci/3276.html 推荐查看天气:http://www.tianqi92.com/danci/3277.html 推荐查看天气:http://www.tianqi92.com/danci/3278.html 推荐查看天气:http://www.tianqi92.com/danci/3279.html 推荐查看天气:http://www.tianqi92.com/danci/3280.html 推荐查看天气:http://www.tianqi92.com/danci/3281.html 推荐查看天气:http://www.tianqi92.com/danci/3282.html 推荐查看天气:http://www.tianqi92.com/danci/3283.html 推荐查看天气:http://www.tianqi92.com/danci/3284.html 推荐查看天气:http://www.tianqi92.com/danci/3285.html 推荐查看天气:http://www.tianqi92.com/danci/3286.html 推荐查看天气:http://www.tianqi92.com/danci/3287.html 推荐查看天气:http://www.tianqi92.com/danci/3288.html 推荐查看天气:http://www.tianqi92.com/danci/3289.html 推荐查看天气:http://www.tianqi92.com/danci/3290.html 推荐查看天气:http://www.tianqi92.com/danci/3291.html 推荐查看天气:http://www.tianqi92.com/danci/3292.html 推荐查看天气:http://www.tianqi92.com/danci/3293.html 推荐查看天气:http://www.tianqi92.com/danci/3294.html 推荐查看天气:http://www.tianqi92.com/danci/3295.html 推荐查看天气:http://www.tianqi92.com/danci/3296.html 推荐查看天气:http://www.tianqi92.com/danci/3297.html 推荐查看天气:http://www.tianqi92.com/danci/3298.html 推荐查看天气:http://www.tianqi92.com/danci/3299.html 推荐查看天气:http://www.tianqi92.com/danci/3300.html 推荐查看天气:http://www.tianqi92.com/danci/3301.html 推荐查看天气:http://www.tianqi92.com/danci/3302.html 推荐查看天气:http://www.tianqi92.com/danci/3303.html 推荐查看天气:http://www.tianqi92.com/danci/3304.html 推荐查看天气:http://www.tianqi92.com/danci/3305.html 推荐查看天气:http://www.tianqi92.com/danci/3306.html 推荐查看天气:http://www.tianqi92.com/danci/3307.html 推荐查看天气:http://www.tianqi92.com/danci/3308.html 推荐查看天气:http://www.tianqi92.com/danci/3309.html 推荐查看天气:http://www.tianqi92.com/danci/3310.html 推荐查看天气:http://www.tianqi92.com/danci/3311.html 推荐查看天气:http://www.tianqi92.com/danci/3312.html 推荐查看天气:http://www.tianqi92.com/danci/3313.html 推荐查看天气:http://www.tianqi92.com/danci/3314.html 推荐查看天气:http://www.tianqi92.com/danci/3315.html 推荐查看天气:http://www.tianqi92.com/danci/3316.html 推荐查看天气:http://www.tianqi92.com/danci/3317.html 推荐查看天气:http://www.tianqi92.com/danci/3318.html 推荐查看天气:http://www.tianqi92.com/danci/3319.html 推荐查看天气:http://www.tianqi92.com/danci/3320.html 推荐查看天气:http://www.tianqi92.com/danci/3321.html 推荐查看天气:http://www.tianqi92.com/danci/3322.html 推荐查看天气:http://www.tianqi92.com/danci/3323.html 推荐查看天气:http://www.tianqi92.com/danci/3324.html 推荐查看天气:http://www.tianqi92.com/danci/3325.html 推荐查看天气:http://www.tianqi92.com/danci/3326.html 推荐查看天气:http://www.tianqi92.com/danci/3327.html 推荐查看天气:http://www.tianqi92.com/danci/3328.html 推荐查看天气:http://www.tianqi92.com/danci/3329.html 推荐查看天气:http://www.tianqi92.com/danci/3330.html 推荐查看天气:http://www.tianqi92.com/danci/3331.html 推荐查看天气:http://www.tianqi92.com/danci/3332.html 推荐查看天气:http://www.tianqi92.com/danci/3333.html 推荐查看天气:http://www.tianqi92.com/danci/3334.html 推荐查看天气:http://www.tianqi92.com/danci/3335.html 推荐查看天气:http://www.tianqi92.com/danci/3336.html 推荐查看天气:http://www.tianqi92.com/danci/3337.html 推荐查看天气:http://www.tianqi92.com/danci/3338.html 推荐查看天气:http://www.tianqi92.com/danci/3339.html 推荐查看天气:http://www.tianqi92.com/danci/3340.html 推荐查看天气:http://www.tianqi92.com/danci/3341.html 推荐查看天气:http://www.tianqi92.com/danci/3342.html 推荐查看天气:http://www.tianqi92.com/danci/3343.html 推荐查看天气:http://www.tianqi92.com/danci/3344.html 推荐查看天气:http://www.tianqi92.com/danci/3345.html 推荐查看天气:http://www.tianqi92.com/danci/3346.html 推荐查看天气:http://www.tianqi92.com/danci/3347.html 推荐查看天气:http://www.tianqi92.com/danci/3348.html 推荐查看天气:http://www.tianqi92.com/danci/3349.html 推荐查看天气:http://www.tianqi92.com/danci/3350.html 推荐查看天气:http://www.tianqi92.com/danci/3351.html 推荐查看天气:http://www.tianqi92.com/danci/3352.html 推荐查看天气:http://www.tianqi92.com/danci/3353.html 推荐查看天气:http://www.tianqi92.com/danci/3354.html 推荐查看天气:http://www.tianqi92.com/danci/3355.html 推荐查看天气:http://www.tianqi92.com/danci/3356.html 推荐查看天气:http://www.tianqi92.com/danci/3357.html 推荐查看天气:http://www.tianqi92.com/danci/3358.html 推荐查看天气:http://www.tianqi92.com/danci/3359.html 推荐查看天气:http://www.tianqi92.com/danci/3360.html 推荐查看天气:http://www.tianqi92.com/danci/3361.html 推荐查看天气:http://www.tianqi92.com/danci/3362.html 推荐查看天气:http://www.tianqi92.com/danci/3363.html 推荐查看天气:http://www.tianqi92.com/danci/3364.html 推荐查看天气:http://www.tianqi92.com/danci/3365.html 推荐查看天气:http://www.tianqi92.com/danci/3366.html 推荐查看天气:http://www.tianqi92.com/danci/3367.html 推荐查看天气:http://www.tianqi92.com/danci/3368.html 推荐查看天气:http://www.tianqi92.com/danci/3369.html 推荐查看天气:http://www.tianqi92.com/danci/3370.html 推荐查看天气:http://www.tianqi92.com/danci/3371.html 推荐查看天气:http://www.tianqi92.com/danci/3372.html 推荐查看天气:http://www.tianqi92.com/danci/3373.html 推荐查看天气:http://www.tianqi92.com/danci/3374.html 推荐查看天气:http://www.tianqi92.com/danci/3375.html 推荐查看天气:http://www.tianqi92.com/danci/3376.html 推荐查看天气:http://www.tianqi92.com/danci/3377.html 推荐查看天气:http://www.tianqi92.com/danci/3378.html 推荐查看天气:http://www.tianqi92.com/danci/3379.html 推荐查看天气:http://www.tianqi92.com/danci/3380.html 推荐查看天气:http://www.tianqi92.com/danci/3381.html 推荐查看天气:http://www.tianqi92.com/danci/3382.html 推荐查看天气:http://www.tianqi92.com/danci/3383.html 推荐查看天气:http://www.tianqi92.com/danci/3384.html 推荐查看天气:http://www.tianqi92.com/danci/3385.html 推荐查看天气:http://www.tianqi92.com/danci/3386.html 推荐查看天气:http://www.tianqi92.com/danci/3387.html 推荐查看天气:http://www.tianqi92.com/danci/3388.html 推荐查看天气:http://www.tianqi92.com/danci/3389.html 推荐查看天气:http://www.tianqi92.com/danci/3390.html 推荐查看天气:http://www.tianqi92.com/danci/3391.html 推荐查看天气:http://www.tianqi92.com/danci/3392.html 推荐查看天气:http://www.tianqi92.com/danci/3393.html 推荐查看天气:http://www.tianqi92.com/danci/3394.html 推荐查看天气:http://www.tianqi92.com/danci/3395.html 推荐查看天气:http://www.tianqi92.com/danci/3396.html 推荐查看天气:http://www.tianqi92.com/danci/3397.html 推荐查看天气:http://www.tianqi92.com/danci/3398.html 推荐查看天气:http://www.tianqi92.com/danci/3399.html 推荐查看天气:http://www.tianqi92.com/danci/3400.html 推荐查看天气:http://www.tianqi92.com/danci/3401.html 推荐查看天气:http://www.tianqi92.com/danci/3402.html 推荐查看天气:http://www.tianqi92.com/danci/3403.html 推荐查看天气:http://www.tianqi92.com/danci/3404.html 推荐查看天气:http://www.tianqi92.com/danci/3405.html 推荐查看天气:http://www.tianqi92.com/danci/3406.html 推荐查看天气:http://www.tianqi92.com/danci/3407.html 推荐查看天气:http://www.tianqi92.com/danci/3408.html 推荐查看天气:http://www.tianqi92.com/danci/3409.html 推荐查看天气:http://www.tianqi92.com/danci/3410.html 推荐查看天气:http://www.tianqi92.com/danci/3411.html 推荐查看天气:http://www.tianqi92.com/danci/3412.html 推荐查看天气:http://www.tianqi92.com/danci/3413.html 推荐查看天气:http://www.tianqi92.com/danci/3414.html 推荐查看天气:http://www.tianqi92.com/danci/3415.html 推荐查看天气:http://www.tianqi92.com/danci/3416.html 推荐查看天气:http://www.tianqi92.com/danci/3417.html 推荐查看天气:http://www.tianqi92.com/danci/3418.html 推荐查看天气:http://www.tianqi92.com/danci/3419.html 推荐查看天气:http://www.tianqi92.com/danci/3420.html 推荐查看天气:http://www.tianqi92.com/danci/3421.html 推荐查看天气:http://www.tianqi92.com/danci/3422.html 推荐查看天气:http://www.tianqi92.com/danci/3423.html 推荐查看天气:http://www.tianqi92.com/danci/3424.html 推荐查看天气:http://www.tianqi92.com/danci/3425.html 推荐查看天气:http://www.tianqi92.com/danci/3426.html 推荐查看天气:http://www.tianqi92.com/danci/3427.html 推荐查看天气:http://www.tianqi92.com/danci/3428.html 推荐查看天气:http://www.tianqi92.com/danci/3429.html 推荐查看天气:http://www.tianqi92.com/danci/3430.html 推荐查看天气:http://www.tianqi92.com/danci/3431.html 推荐查看天气:http://www.tianqi92.com/danci/3432.html 推荐查看天气:http://www.tianqi92.com/danci/3433.html 推荐查看天气:http://www.tianqi92.com/danci/3434.html 推荐查看天气:http://www.tianqi92.com/danci/3435.html 推荐查看天气:http://www.tianqi92.com/danci/3436.html 推荐查看天气:http://www.tianqi92.com/danci/3437.html 推荐查看天气:http://www.tianqi92.com/danci/3438.html 推荐查看天气:http://www.tianqi92.com/danci/3439.html 推荐查看天气:http://www.tianqi92.com/danci/3440.html 推荐查看天气:http://www.tianqi92.com/danci/3441.html 推荐查看天气:http://www.tianqi92.com/danci/3442.html 推荐查看天气:http://www.tianqi92.com/danci/3443.html 推荐查看天气:http://www.tianqi92.com/danci/3444.html 推荐查看天气:http://www.tianqi92.com/danci/3445.html 推荐查看天气:http://www.tianqi92.com/danci/3446.html 推荐查看天气:http://www.tianqi92.com/danci/3447.html 推荐查看天气:http://www.tianqi92.com/danci/3448.html 推荐查看天气:http://www.tianqi92.com/danci/3449.html 推荐查看天气:http://www.tianqi92.com/danci/3450.html 推荐查看天气:http://www.tianqi92.com/danci/3451.html 推荐查看天气:http://www.tianqi92.com/danci/3452.html 推荐查看天气:http://www.tianqi92.com/danci/3453.html 推荐查看天气:http://www.tianqi92.com/danci/3454.html

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码