Cactching Stdout

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Massi

    #1

    Cactching Stdout

    Hi everyone! I'm writing a python script which uses a C-written dll. I
    call the functions in the dll using ctypes, but I don't know how to
    catch the output of the "printf" which the C functions use. In fact I
    don't even know if it is possible! I've heard something about PIPE and
    popen...is this what I need? How can I use them? It is very important
    for me that I could take the output in real-time.
    Thanks for the help!
    Massi

  • Gabriel Genellina

    #2
    Re: Cactching Stdout

    At Wednesday 8/11/2006 06:09, Massi wrote:
    >Hi everyone! I'm writing a python script which uses a C-written dll. I
    >call the functions in the dll using ctypes, but I don't know how to
    >catch the output of the "printf" which the C functions use. In fact I
    >don't even know if it is possible! I've heard something about PIPE and
    >popen...is this what I need? How can I use them? It is very important
    >for me that I could take the output in real-time.
    Since you are calling a function the same process, popen&co won't help.
    This is just an idea; printf is writting to STDOUT; you could replace
    STDOUT with the write end of a pipe, and read from the other end.


    --
    Gabriel Genellina
    Softlab SRL

    _______________ _______________ _______________ _____
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

    Comment

    • Stephan Kuhagen

      #3
      Re: Cactching Stdout

      Dennis Lee Bieber wrote:
      >popen...is this what I need? How can I use them? It is very important
      >for me that I could take the output in real-time.
      >Thanks for the help!
      >Massi
      >
      That's going to be difficult... popen and pipes work when one
      process starts another INDEPENDENT process.
      I'm not sure, if it fits the problem, but just a few days ago I had the
      problem to catch the stdout and stderr of a module to process it before
      writing it to the console. What I did was basically this:

      ---
      import sys
      import StringIO

      # save the original streams
      _stdout_ = sys.stdout
      _stderr_ = sys.stderr

      # create StringIO string buffers to replace streams
      sys.stdout = StringIO.String IO()
      sys.stderr = StringIO.String IO()

      # Now print something, this goes to the string buffers instead of console
      print "Hello World to stdout"
      sys.stderr.writ e("Hello World to stderr\n")

      # Now process the contents of the buffers...
      ....
      # ...and print them to the real console afterwards
      _stdout_.write( sys.stdout)
      _stderr_.write( sys.stderr)

      # Clean up the string buffers for the next IO
      sys.stdout.trun cate(0)
      sys.stderr.trun cate(0)
      ---

      Of course you should put all that into defs and create own write/print
      functions to encapsulate the whole buffering/processing/cleanup.

      This works great for scripting, but I'm pretty sure, that it does not work
      for C Libraries in Python. But I do not know, how Python handles its
      streams internally, so it might be worth a try.

      Regards
      Stephan

      Comment

      Working...