help-octave
[Top][All Lists]
Advanced

[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: Michael Goffioul
Subject: Re: [example] how to get popen2() working? [XP, MSVC 2.9.15-octave]
Date: Sat, 1 Dec 2007 15:35:36 +0100

On 11/30/07, andreas.naessl <address@hidden> wrote:
> hi all,
>
> i try to establish a simple bidirectional pipe-connection between octave and
> a little "server"-programm written in python, for direct data-transfer without
> producing temp-file-trash; i thought of using octave's popen2(). the server
> should be running continuously in the background, waiting for requests.
>
> but i cannot get it working really.
>
> [my System: win XP + Michaels MSVC-octave 2.9.15]
>
> first: if i communitcate to my "server" directly at octave-prompt,
> i get something back
> (although i don't understand the need of the fclear(b) command
> when i want a second readline back from my server-app.)
> please see octave-commandline-output below:
>
>  octave.exe:35> [a,b,i]=popen2('python','svr_02.py')
>  a =  8
>  b =  7
>  i =  20204
>  octave.exe:36> fputs(a,"test_command_at promt\n")
>  ans = 0
>  octave.exe:37> fgets(b)
>  ans = got command  test_command_at promt    ### ok
>
>  octave.exe:38> fputs(a,"0\n")
>  ans = 0
>  octave.exe:39> fgets(b)
>  ans = -1                        ### why this ? ###
>  octave.exe:40> fclear(b)
>  octave.exe:41> fgets(b)
>  ans = got command  0           ### ok
>
>  octave.exe:42> fclose(a)
>  ans = 0
>  octave.exe:43> fclose(b)
>  ans = 0
>
> second: if i put the same commands into a m-file (octave_cli.m),
> i don't get back anything to read. why this?
>
> code of "octave_cli.m":
>  [a,b,i]=popen2('python','svr_02.py')
>  # [a,b,i]=popen2('python',['-u','svr_02.py']) # unbuffered python doesn't 
> work
>  fputs(a,"testcommand\n")
>  fgets(b)
>  fputs(a,"0\n")
>  # fgets(b)   gives on prompt here: ans = -1, so ...
>  fclear(b)
>  fgets(b)    # ...after fclear: ans = got command  0, but only on prompt 
> directly
>  fclose(a)
>  fclose(b)
>
> gives the output:
>  octave.exe:34> octave_cli
>  a =  9
>  b =  8
>  i =  20360
>  ans = 0
>  ans = -1    # here i should get back my sent command-string
>  ans = 0
>  ans = -1    # here also
>  ans = 0
>  ans = 0

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.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]