javascript - 如何在超时后杀死 spawnSync
问题描述
我想杀死 ES6 async / await 中的 spawnSync 进程。
(async () => {
const type = 'python';
const exefile = './test.py';
let opt = [file];
let result = await spawnSync(type, opt, {
encoding: 'utf-8'
});
if (exefile !== '') {
const exeRst = await spawnSync(exefile, {
encoding: 'utf-8'
});
setTimeout(() => {
console.log('⏰ Timeout!!');
console.log('exeResult.pid : ', exeResult.pid);
exeResult.kill();
}, 2000);
if (
result.output['1'] === '' &&
result.output['2'] === '' &&
exeRst.output['1'] !== ''
) {
console.log('exeResult:', exeRst);
console.log('result:', result.output);
}
}
})();
如果第二个 spawnSync exeRst 需要很长时间,它将在 2 秒内停止执行。
test.py 需要 10 秒或更长时间才能运行。
但是,由于等待,setTimeout 将在 test.py 的所有执行结束后的 10 秒后执行。
如何让跑步不能超过 2 秒?
解决方案
spawnSync支持一个名为timeout
. 这以毫秒为单位指定允许进程运行多长时间:
await spawnSync(exefile, {
encoding: 'utf-8',
timeout: 2000
});
推荐阅读
- c# - 如何在单击按钮时删除 RichTextBox 中的单词?
- netlogo - NetLogo根据行为空间中的重复次数设置随机种子
- linux - 如何在 Virtual Box Linux GuestOS 上运行嵌套虚拟化(即 KVM Android 模拟器)?
- java - JBoss 和 Eclipse 错误:Java 运行时环境内存不足,无法继续
- css - 有什么办法元素溢出它的父级
- c - Minifilter 在特定情况下丢失已删除的文件 Windows 10 1903 +
- android - 使用 glide 从 firebase 获取图像并设置为回收器视图但图像未加载
- c++ - 如何找到这个函数的执行次数?
- java - 如何将 100 转换为一零零不是一?
- asp.net-core - 特定的 API 调用在 .net core 3.1 的 chrome 中停滞不前