Qute I/O

Been meandering about with Pythons subprocess and various IPC (inter Process Communication) focuses modules tonight. When I remembered I had noted that QT and KDE had classes for working with processes.

Looked up the class in the documentation and played with it. Then I found a small example program that they included. Here is the QT written example using C++, the primary language.


/****************************************************************************
** $Id: qt/process.cpp 3.3.7 edited Aug 31 2005 $
**
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <qobject.h>
#include <qprocess.h>
#include <qvbox.h>
#include <qtextview.h>
#include <qpushbutton.h>
#include <qapplication.h>
#include <qmessagebox.h>

#include <stdlib.h>

class UicManager : public QVBox
{
Q_OBJECT

public:
UicManager();
~UicManager() {}

public slots:
void readFromStdout();
void scrollToTop();

private:
QProcess *proc;
QTextView *output;
QPushButton *quitButton;
};

UicManager::UicManager()
{
// Layout
output = new QTextView( this );
quitButton = new QPushButton( tr("Quit"), this );
connect( quitButton, SIGNAL(clicked()),
qApp, SLOT(quit()) );
resize( 500, 500 );

// QProcess related code
proc = new QProcess( this );

// Set up the command and arguments.
// On the command line you would do:
// uic -tr i18n "small_dialog.ui"
proc->addArgument( "uic" );
proc->addArgument( "-tr" );
proc->addArgument( "i18n" );
proc->addArgument( "small_dialog.ui" );

connect( proc, SIGNAL(readyReadStdout()),
this, SLOT(readFromStdout()) );
connect( proc, SIGNAL(processExited()),
this, SLOT(scrollToTop()) );

if ( !proc->start() ) {
// error handling
QMessageBox::critical( 0,
tr("Fatal error"),
tr("Could not start the uic command."),
tr("Quit") );
exit( -1 );
}
}

void UicManager::readFromStdout()
{
// Read and process the data.
// Bear in mind that the data might be output in chunks.
output->append( proc->readStdout() );
}

void UicManager::scrollToTop()
{
output->setContentsPos( 0, 0 );
}

int main( int argc, char **argv )
{
QApplication a( argc, argv );
UicManager manager;
a.setMainWidget( &manager );
manager.show();
return a.exec();
}

#include "process.moc"

Needless to say, I am not really a fan of C++ at times ^_^. I translated the example into Python for a simple test and I like it quite a bit.

#!/usr/local/bin/python

# I translated the example: process/process.cpp to python and boy do I love qt!
#
#############################################################################
##
## Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
##
## This file is part of an example program for Qt. This example
## program may be used, distributed and modified without limitation.
##
##############################################################################

import sys
from qt import *

class UicManager(QVBox):
def __init__(self):
QVBox.__init__(self)
self.output = QTextView(self)
self.quitButton = QPushButton(self.tr('Quit'), self)
self.connect(self.quitButton, SIGNAL('clicked()'), qApp,
SLOT('quit()'))
self.resize(500,500)

# QProcess related code
self.proc = QProcess(self)

self.proc.addArgument("uic")
self.proc.addArgument("form1.ui")

self.connect(self.proc, SIGNAL('readyReadStdout()'),
self.readFromStdout)
self.connect(self.proc, SIGNAL('processExited()'),
self.scrollToTop)

if not self.proc.start():
sys.stderr.write('fatal error could not start uicn')
sys.exit(1)

def readFromStdout(self):
'read in proc data which may be in chucks'
self.output.append(str(self.proc.readStdout()))

def scrollToTop(self):
self.output.setContentsPos(0,0)

if __name__ == "__main__":
a = QApplication(sys.argv)
manager = UicManager()
a.setMainWidget(manager)
manager.show()
sys.exit(a.exec_loop())

QProcess is nice but I’m not sure about using enormous amounts of data though yet.

EDIT:

As a test I set up the Python script to pass a ‘ls -R /’ basically causing it to list all files on all mounted file systems. The code to scroll back to the top caused major lock up to the program and some performance loss for the entire laptop: probably screaming at being down to about a 150MB of swap space left.

Removing that and closing programs so only konsole running it and top + amarok playing music was running. At times top reported free memory over < 1MB about 30-40% swap usage. However without the scroll to top thing gone it didn't cause any real performance for the laptop; probably because there was much less swap usage used up. As a test I ran ls -R / direct afterwards and while it did pile on the stats in top, it still kept to about 50MB free and even less swap usage on the displays.
So far I think it would probably work for whatever is needed as long as the computer continues to provide adequate memory and the kernel doesn’t have a hissy fit about the pipes and things in the background.