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: Muthiah Annamalai
Subject: Re: [example] how to get popen2() working? [XP, MSVC 2.9.15-octave]
Date: Fri, 30 Nov 2007 13:50:40 -0600
User-agent: Thunderbird 2.0.0.6 (X11/20071022)

andreas.naessl 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

third - just to be complete and going a bit octave-off-topic:
the code the pythonic testing-server "svr_02.py"
is added below. does anybody see a problem there? with a python-client-connection i managed it working.

  import sys
  # little test-server-app., just echo-ing incoming commands
  # command "0" terminates server
  def main():
    for line in iter(sys.stdin.readline,""):
      ln = line
      print "got command ", ln.rstrip('\n')   # answer-echo on stdout; remove 
newline
      # later insert data-transfer here
      if ln == "0\n":
        print ln, "exit.."
        sys.exit(0)
sys.stdout.flush() # important sys.stdin.flush() if __name__ == "__main__":
    main()

sorry for the long msg... the prob might be just a stupid mistake by me, as i'm not really a programming-expert...
anyway, i would strongly appreciate any hints by the community

thanks, best regards
andreas
My message is not directly related to solving your problem.
I think tk_octave package by Paul Kienzle, from octave-forge does
something like this; just between tk and octave using sockets.
Maybe you want to look at that source code.

-Muthu




reply via email to

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