4

I'm trying to execute a program (binary), that is printing values regularly on the stdout.

with exec, it works fine, but with spawn, results only comes after a while, why ? what can I do about that ?

  const opcv=spawn("myprog",["-L","2","-o","pairs","-r"], { shell:false, stdio:"pipe" })
  opcv.stdout.on('data', (data) => {
      node.log("opcdata stdout=" + data);
    });
  opcv.stderr.on('data', (data) => {
      node.log("opcdata stderr=" + data);
    });

I tried whith the option {stdio:"inherit"}, in that case I see the data directly, but I'm not able to work with ".stdout.on(" anymore :

TypeError: Cannot read property 'on' of null

EDIT It seems to be linked with the program I'm trying to run, but unfortunately its not possible to disable stdout buffering in the program, is it possible to execute it with another shell than cmd , or are they other tricks ?

OpenStove
  • 714
  • 1
  • 11
  • 22
  • I use spawn(), and piping. Only difference is I use pause & resume to prevent an buffer overruns. What is node.log()?, also if it's binary data, most logging would struggle. try putting console.log(data.length); do you then get a constant stream.? – Keith Oct 06 '16 at 12:06
  • No the data coming out is string. node.log is a logging function, within node-red, and as it is not executing on the client-side, console.log won't help. What do you mean using pause&resume ? For me it seems that I only get the stdout when it is flushing – OpenStove Oct 06 '16 at 12:31
  • Inside my on('data') event, I will stdin.pause(), consume the data, and then stdin.resume(); This prevents buffer overruns.. I use this to talk to a binnary protocol between node.js & a Delphi App, it's very fast. Are you sure the program your calling is not buffering it's stdout? – Keith Oct 06 '16 at 13:01
  • I'm not sending anything to stdin... I'm not sure how to check if the program is buffering, but what I know is if I execute it manually, it print new lines directly to stdout, no need to wait like using spwan – OpenStove Oct 06 '16 at 13:18
  • I'm not saying you are sending anything to stdin, the pause it to make the other end wait until you have consumed the data, the resume then tells the other end to resume sending data. Stdio / Stdout have limited buffer space, so there is potential to run out if not used. Because your not doing much with the data atm, it's most likely not your problem. But say you started pumping things into a database etc, and you needed to wait until the record was saved, stdin.pause() is good for that. To add, the program your calling might act differently in regards to buffer & flushing in console. – Keith Oct 06 '16 at 13:37
  • Seems really to be linked with the program I try to run, but I cannot change it... I need to find a workaround.. But is it possible at all that it is buffered, if I see the output directly in cmd, when running manually ? – OpenStove Oct 06 '16 at 15:38
  • Did you find a solution or workaround? I have pretty much the exact same problem – Cowas Jan 26 '22 at 16:56

0 Answers0