如何将nodejs windows服务设置为高优先级

How to set nodejs windows service as high priority

我有一个节点服务器和脚本,使用 https://github.com/jon-hall/pm2-windows-service 和 pm2 作为 windows 服务运行。

服务器对时间很敏感,我看到硬件达到 100% CPU 使用率并且脚本计时停止的情况,有时会延迟几分钟。

我想通过将服务设置为高优先级来缓解这种情况。我该怎么做?

我找到了一些涉及 VBScript 的解决方案 https://serverfault.com/questions/179161/change-windows-service-priority,但是是否有 pm2 或节点方式来设置服务或在运行后提升自身?

此外,如果我将其发布在错误的站点上,请发表评论。有点灰,既是服务器问题又是编程问题

我能够使用此代码完成它并结束我的加载过程。

const {exec} = require('child_process');
exec('wmic process where "ProcessId=' + process.pid + '" CALL setpriority 256');

您可以使用 os.setPriority:

import os from 'node:os';
os.setPriority(process.pid, os.constants.priority.PRIORITY_HIGHEST);