javascript - 并行执行命令 - nodejs
问题描述
我需要一一执行许多命令,例如:
for(let i = 0; i < 1250; i++) {
spawn('cp', [`${myparam[i]}`, `${anotherParam[i]}`])
}
当然我得到了Error: spawn /bin/sh EAGAIN
。我觉得这不是一个好方法。我的 cmd 必须包含有关数组中项目的一些信息。做到这一点的最佳方法是什么?谷歌对这种情况一无所知......
确切地说:我需要使用 mustache 解析大约 200 个 html 文件。我是通过 CLI 完成的,例如:
spawn('mustache', ['template.json', '${input}.html', '${output}.html'])
解决方案
您可以将 mustache API 与graceful-fs
.
将您的命令替换为Mustache.render
const fs = require("graceful-fs");
const Mustache = require("mustache");
const viewFile = "./template.json";
const input = ["input.html"];
const output = ["output.html"];
fs.readFile(viewFile, "utf8", (err, viewJson) => {
if (err) throw err;
const view = JSON.parse(viewJson);
for (let i = 0, len = input.length; i < len; i++) {
fs.readFile(input[i], "utf8", (readErr, template) => {
if (readErr) throw readErr
fs.writeFile(output[i], Mustache.render(template, view), writeErr => {
if (writeErr) throw writeErr;
});
});
}
});
推荐阅读
- terraform - 如何对外部程序隐藏 AWS 凭证?
- css - 图像最大宽度属性导致意外行为
- python - 为什么 datetime 在 Windows 上无法转换为时间戳 < 86400?
- python-3.x - 可以使用 subprocess.Popen 函数将字典作为参数发送吗?
- file - Shell 脚本 [使用 Grep 查找文件并提取特定模式行]
- c# - 如何在@Html.TextBoxFor 上使用“readonly = true 属性”
- javascript - 在 JavaScript 中用文本打印数组的所有元素
- html - 触发事件后卡片的颜色不会改变
- css - 有没有办法以正确的文本纵横比显示超过 3 个屏幕的 16:9 网页,并且没有明显的像素化?
- java - 从 jar 中解压缩单个文件会给出 java.util.zip.ZipException: invalid CEN header (bad signature)