By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,914 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,914 IT Pros & Developers. It's quick & easy.

Cactching Stdout

P: n/a
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

Nov 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 8 '06 #2

P: n/a
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.StringIO()
sys.stderr = StringIO.StringIO()

# Now print something, this goes to the string buffers instead of console
print "Hello World to stdout"
sys.stderr.write("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.truncate(0)
sys.stderr.truncate(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
Nov 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.