Command line and GUI tools : need a single threading solution
Adrian Casey
news at outbacklinux.com
Tue Jan 11 04:09:26 EST 2005
Phil Thompson wrote:
>> I have a collection of multi-threaded command line tools which I want
>> wrap a
>> PyQt gui around. I'm using queues to route messages from the command
>> line tools to the PyQt gui. The command line tools use python threads to
>> do
>> their work. The gui uses a QThread object to read incoming messages.
>>
>> This does not work consistently - and I've since read that mixing python
>> threads and QThreads is a bad idea. The command line tools work well
>> using
>> python threads.
>
> How well mixing the two threading APIs works depends on version numbers. A
> PyQt generated with SIP v4.x (rather than SIP v3.x) with Python v2.4
> should be Ok.
>
>> I don't want to maintain two copies of the tools - one for command line
>> and
>> another for the PyQt version.
>>
>> I'm thinking it may be possible to modify the command line tools to use
>> qt
>> threads instead of native python threads. Is this the way to go? Are
>> there other options?
>
> Using QThreads only should work - just tell the QApplication ctor that you
> have a console application.
>
> Phil
Phil,
I'm running python 2.4, sip-4.0 and PyQt-x11-gpl-3.11 (or .13 - not sure as
I'm not at work).
A little more detail.
My script, if invoked from the command line, creates a queue object (let's
call it logQueue) which is used to take the output from each individual
thread. If invoked via the gui, the script is passed a logQueue created by
the gui. Each thread dumps its output using logQueue.put(). There are
other queues which are used to service threaded access to a database etc.
The scripts use the pexpect module to login to remote systems and do
sysadmin tasks (e.g. reset passwords). This works like a charm in console
mode.
The gui uses a QThread to poll logQueue (logQueue.get()). When it gets
something, it creates a QCustomEvent and sets the data portion using the
data read from the logQueue. I have a customEvent method in my application
which grabs the data from the customEvent and processes it accrodingly
(writing output to a QTable).
The gui locks up after an arbitrary number of rows have been inserted in the
QTable. It is not consistent. Sometimes it does not lock at all.
I have a non-threaded set of the command line tools which run perfectly with
the gui.
Adrian.
More information about the Python-list
mailing list