[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [example] how to get popen2() working? [XP, MSVC 2.9.15-octave]
From: |
andreas.naessl |
Subject: |
Re: [example] how to get popen2() working? [XP, MSVC 2.9.15-octave] |
Date: |
Sun, 2 Dec 2007 17:55:49 +0100 |
> Basically, your problem comes from the fact that the streams you get
> back from popen2 are in non-blocking mode and that once an error
> occured on the read end, you need to clear its state to be able to read
> again. When you put your code into a m-file, you should
> 1) use fflush after writing something in the write-end, to be sure the data
> arrived to the child process
> 2) maybe wait a little bit before trying to read something on the read-end,
> to be sure the child process has processed the data and answered back;
> if an error occured, use fclear to clear the stream state
>
> Alternatively, you can use an (apparently) undocumented feature of the
> popen2 function: you can provide a 3rd argument to specify the pipe mode;
> if true, the pipes are blocking, if false (the default), the pipes are
> non-blocking.
> In non-blocking mode, be aware that you may block octave if there's nothing
> to read on the read-end.
>
> Michael.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
hello. below is my m-file code, things are working now with my example.
But i stll don't understand, why the popen2-help-example (the "sort -r")
doesn't work. i played a bit around, but it didn't do...
i have the feeling the "sort" behaviour might be the reason...
at oct-promt with: system('sort -r') i need to type a final "ctrl-D"
after the inputlines to get my sorted results back -
do we send it in the popen2-example?
andreas
----
# testprogramm kommunikation mit python server-programm via pipe popen2
0;
function r=snd(fid,s);
r=fputs(fid,[s,"\n"]); # newline for string added here
fflush(fid);
endfunction
function r=rcv(fid);
while (ischar(r=fgets(fid)) == false)
fclear(fid); # endless loop; i'm shure there comes sth. to read otherwise
hang up
endwhile # maybe implement later to jump out when waiting too long
for a reply
endfunction
[a,b,i]=popen2('python','svr_02.py');
snd(a,"testcmd 1");
rcv(b) # get back echo
snd(a,"testcmd 2");
rcv(b) # get back echo
snd(a,"0"); # "0" = shutdown server-prg
rcv(b)
fclose(a);
fclose(b);