pseudo terminal usage from Python?

Grant Edwards invalid at invalid
Tue Dec 23 11:03:49 EST 2008


On 2008-12-23, skip at pobox.com <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.

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

> 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.

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

-- 
Grant Edwards                   grante             Yow! Please come home with
                                  at               me ... I have Tylenol!!
                               visi.com            



More information about the Python-list mailing list