pseudo terminal usage from Python?

S

skip

I ran into an interesting problem yesterday. The mpstat(1) command on
Solaris formats its output like so:

CPU minf mjf xcal intr ithr csw icsw migr smtx srw syscl usr sys wt idl
0 42 1 1184 812 265 227 12 44 37 0 1131 6 2 0 93
1 25 1 933 447 2 203 37 75 12 0 902 5 4 0 91
2 17 0 195 495 1 201 41 77 13 0 514 5 1 0 94
3 4 0 117 882 405 171 34 65 21 0 449 5 2 0 93

I'm only interested in presenting the CPU numbers and user+sys values
prefixed by a timestamp. For example, the above might be formatted like so:

07:28:46.373328 0 8 1 9 2 6 3 7

The obvious solution might be something simple like this:

mpstat 1 | python mympstat.py

where mympstat.py does a trival amount of reformatting.

The problem is that mpstat recognizes when its output is a pipe and block
buffers it so the Python script sees input in massive blobs, not the
second-by-second output you'd see running "mpstat 1" by itself. I've been
reduced to a much more complicated solution which involves forking mpstat
with output to a file, then reading the end of that file every second. A
three-line Python script balloons into a one-page script. Yuck. Add to
that I'm writing this for an admin who is considering Python as a scripting
language. Double Yuck. (But not nyuk nyuk, this is not the Stooges.)

I suspect there is some magic I can perform with pseudo terminals (this is
on Solaris 10.) The documentation for the pty module contains no examples
and I've been so far unable to find any using Google.

Any pointers/examples? I will gladly add an example to the pty module docs
(I have the power!) once I have a couple working examples (maybe one example
each of reading and writing?)

Thanks,
 
S

skip

Grant> Are you sure it's not Python buffering its input? Have you tried
Grant> "python -u mympstat.py"?

Hmmm... No, I hadn't considered that. I'll check it out. Thanks...

Grant> I had a Linux pty example running once upon a time. If "python
Grant> -u" doesn't work post again, and I'll try to find it.

Would be helpful even if I don't need it for this task, as I've not been
able to find any simple examples on the net. (Maybe there's a recipe at
ActiveState, but Google didn't pick up anything with the search terms I was
using.)

Skip
 
S

skip

Grant> Are you sure it's not Python buffering its input? Have you tried
Grant> "python -u mympstat.py"?

Nope. -u unbuffers stdout and stderr, not stdin. It really must be mpstat
being uncooperative.

Skip
 
S

skip

Grant> Are you sure it's not Python buffering its input? Have you tried
Grant> "python -u mympstat.py"?

Grant> That's not what my python man page says:

Grant> -u Force stdin, stdout and stderr to be totally
Grant> unbuffered. On systems where it matters, also put
Grant> stdin, stdout and stderr in binary mode.

Grant> That's for 2.5.2, but that's how I remember previous versions
Grant> working as well.

I'm still running 2.4.5 at work. It's -u help:

-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)
see man page for details on internal buffering relating to '-u'

Was stdin unbuffering maybe added in 2.5?

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top