pseudo terminal usage from Python?
Steve Holden
steve at holdenweb.com
Tue Dec 23 09:21:13 EST 2008
skip at pobox.com wrote:
> 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?)
>
Look at the pexpect module - you can run interactive tasks through that.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list