NodeJS直到stdin关闭后才从stdin上的管道运行代码

问题描述 投票:2回答:1

我有一个运行js代码的python程序。但是,节点js直到stdin关闭后才运行它收到的任何代码,即使syscall跟踪确认它已成功从stdin读取了该内容。有关更多信息,请参见here (The accepted answer)。在关闭stdin之前有什么方法可以运行命令?

这也可以在不使用Python的情况下使用bash进行复制,如下所示:

{
  echo 'console.log("HI");'
  sleep 1
  echo 'console.log("HI");'
  sleep 1
  echo "Sent both commands to node with one second delay after each" >&2
} | node

其输出是:

Sent both commands to node with one second delay after each
HI
HI

...如果代码在收到代码后立即由Node执行,则会看到:

HI
HI
Sent both commands to node with one second delay after each
node.js stdin
1个回答
0
投票
const fs = require('fs'); while (true){ const data = fs.readFileSync("myinput", "utf8"); if (data){ try{ console.log(data); eval(data) } catch(err) { console.log(err) }; fs.writeFileSync("myinput", "", "utf8") }; };

然后我将启动程序并将其用作标准输入写入文件myinput

© www.soinside.com 2019 - 2024. All rights reserved.