R
Rüdiger Ranft
Hi all,
I need to call some programms and catch their stdout and stderr streams.
While the Popen class from subprocess handles the call, I get the
results of the programm not until the programm finishes. Since the
output of the programm is used to generate a progress indicator, I need
a way to acces the values written to stdout/stderr as fast as possible.
Beneath is a test which shows what I did
TIA
Rudi
----8<-------8<-------8<-- iodummy.cpp -8<-------8<---
#include <iostream>
#include <unistd.h>
int main()
{
for( int i = 0; i < 10; i++ )
{
std::cerr << i << std::endl;
sleep(2);
}
}
from subprocess import Popen, PIPE
from time import sleep
p = Popen('./iodummy',stdin=PIPE, stdout=PIPE, stderr=PIPE)
sleep(3)
# now I expect '0\n1\n' in stderr, but read() blocks until
# the end of iodummy.
print p.stderr.read()
p.wait()
--
GPG encrypted mails preferred.
GPG verschlüsselte Mails bevorzugt.
---> http://chaosradio.ccc.de/media/ds/ds085.pdf Seite 20 <----
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iQIcBAEBAwAGBQJJ5xKqAAoJEI+IiZON95WTR5YP/iymEmF37qbnnMSSQZFs629Q
sSLrcjNytEmuQZvYdvsrJJ2LiR4NHzLTC+zVVP41VsOwfydfAFZhWBuKZTN2pqUT
jfZAYM1KALoy0f9Z9gD08ehTwrXy5+1ZRmj3aLmdqSmD6Jqjfnz3495VivB2f9JY
aYT8ZGglikVDeiTO8GekA7Lyfq+DItZ8jXcSxlQqiz6ega0DapL6NHH6c9d5nx+F
qAc7/JERZsMNd8EMDp6tb8xDVFp/ZIzSS3aYFRmQHfdE8lSp5DG2868gzEVU/h5Y
0D25gs/tAmuYFyutMq1GulDwLcpZWFqjidibav6M6XabvL45QcsKrsAlDQrLin/x
JwDct72lgseh2JrPx4Kcdz1/+4uppMuPYbnJ1YMTsGxthMXyP0Wk2RVITrVHoH2P
j4GwuNPGiEu3rkNutUUer4wkQVUnss/D71jNYXl9SgmoMsq9Ub66p8GO9vepgWlw
NK/7U5iad96ETHJgcvWhx8o/ymr8V6kOBqQpLlRCNsd/2MRQvoT61tmK0UDdx2lL
x/LrhZftbAiVN5hzzAC2H6nX02m9bipfqWKbzvp71wbLZufXAo2R8jSmibrdUQff
16TUVeIhUY4YIuIscG6YyTMit5tEUTtCL2Mi3yTFe8R8uOR3h/1t1DbhDBYRtQAh
Sa8efc/lkb8gLUJU3nnz
=xaSm
-----END PGP SIGNATURE-----
I need to call some programms and catch their stdout and stderr streams.
While the Popen class from subprocess handles the call, I get the
results of the programm not until the programm finishes. Since the
output of the programm is used to generate a progress indicator, I need
a way to acces the values written to stdout/stderr as fast as possible.
Beneath is a test which shows what I did
TIA
Rudi
----8<-------8<-------8<-- iodummy.cpp -8<-------8<---
#include <iostream>
#include <unistd.h>
int main()
{
for( int i = 0; i < 10; i++ )
{
std::cerr << i << std::endl;
sleep(2);
}
}
from subprocess import Popen, PIPE
from time import sleep
p = Popen('./iodummy',stdin=PIPE, stdout=PIPE, stderr=PIPE)
sleep(3)
# now I expect '0\n1\n' in stderr, but read() blocks until
# the end of iodummy.
print p.stderr.read()
p.wait()
--
GPG encrypted mails preferred.
GPG verschlüsselte Mails bevorzugt.
---> http://chaosradio.ccc.de/media/ds/ds085.pdf Seite 20 <----
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iQIcBAEBAwAGBQJJ5xKqAAoJEI+IiZON95WTR5YP/iymEmF37qbnnMSSQZFs629Q
sSLrcjNytEmuQZvYdvsrJJ2LiR4NHzLTC+zVVP41VsOwfydfAFZhWBuKZTN2pqUT
jfZAYM1KALoy0f9Z9gD08ehTwrXy5+1ZRmj3aLmdqSmD6Jqjfnz3495VivB2f9JY
aYT8ZGglikVDeiTO8GekA7Lyfq+DItZ8jXcSxlQqiz6ega0DapL6NHH6c9d5nx+F
qAc7/JERZsMNd8EMDp6tb8xDVFp/ZIzSS3aYFRmQHfdE8lSp5DG2868gzEVU/h5Y
0D25gs/tAmuYFyutMq1GulDwLcpZWFqjidibav6M6XabvL45QcsKrsAlDQrLin/x
JwDct72lgseh2JrPx4Kcdz1/+4uppMuPYbnJ1YMTsGxthMXyP0Wk2RVITrVHoH2P
j4GwuNPGiEu3rkNutUUer4wkQVUnss/D71jNYXl9SgmoMsq9Ub66p8GO9vepgWlw
NK/7U5iad96ETHJgcvWhx8o/ymr8V6kOBqQpLlRCNsd/2MRQvoT61tmK0UDdx2lL
x/LrhZftbAiVN5hzzAC2H6nX02m9bipfqWKbzvp71wbLZufXAo2R8jSmibrdUQff
16TUVeIhUY4YIuIscG6YyTMit5tEUTtCL2Mi3yTFe8R8uOR3h/1t1DbhDBYRtQAh
Sa8efc/lkb8gLUJU3nnz
=xaSm
-----END PGP SIGNATURE-----