Yes, please see my code as pseudo code. The solution I am working with is
actually fully async and reads as much into the buffer as possible and when
this is done, goes into the setTimeout polling.
I find your suggestion interesting, but I believe it will not work because
the file stream will not emit on('data') events when the spawned process
writes into the file. At least, this is what I tried some time ago and I
believe I was not pinged when something got written to the file. Did this
change in node 0.8 with pipe?
On Thursday, May 9, 2013 3:33:44 PM UTC+2, Matt Sergeant wrote:
>
> It should work fine everywhere. But be aware you're using readSync. You
> should also read as much data as is available, and then do your
> setTimeout().
>
> Another option would be to clone the stdOutFd into a ReadStream and pipe()
> it to process.stdout:
>
> var stream = fs.createReadStream(null, { fd: stdOutFd, encoding:
> 'utf8' });
> stream.pipe(process.stdout);
>
> Matt.
>
>
> On Thu, May 9, 2013 at 5:54 AM, Benjamin Pasero
> <[email protected]<javascript:>
> > wrote:
>
>> Hi,
>>
>> my scenario is this: From my node application I am spawning another
>> program. I want this spawned programs output piped to a file, so I pass a
>> file descriptor through the options.stdio array:
>>
>> var stdOutFd = fs.openSync("stdout.txt", 'w+');
>> var result = cw.spawn("myprogram.exe", [], { stdio: ['ignore', stdOutFd,
>> 'ignore']});
>>
>> This works nice. Once my program has finished, I can see the output in my
>> stdout.txt.
>>
>> Now, this is nice for short running programs, but if myprogram.exe runs
>> over a minute I would like to show constant output while the program is
>> running and not only at the end. So here is my question: Is it safe to
>> re-use the stdOutFd from my main application and periodically read it
>> for output? Something like:
>>
>> var stdOutFd = fs.openSync("stdout.txt", 'w+');
>> var result = cw.spawn("myprogram.exe", [], { stdio: ['ignore', stdOutFd,
>> 'ignore']});
>>
>> function readOutPut() {
>> var buffer = ...
>> var bytesRead = fs.readSync(stdOutFd, buffer...)
>> console.log(buffer.toString());
>>
>> setTimeout(readOutPut, 1000); // Poll every second
>> }
>>
>> readOutPut();
>>
>> I should say that this code executes fine on Windows. I would like to
>> hear back from someone that has more experience if this is fragile code. An
>> alternative would be to open the file a second time for reading (using "r"
>> as mode since I only want to read), but I am not sure if that is actually
>> needed.
>>
>> The reason I am worried is that I am not sure how this code would behave
>> under different filesystems. Especially I am uncertain if using a UNC share
>> would make a difference.
>>
>> Thanks,
>> Ben
>>
>> --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]<javascript:>
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.