pyqt4: multi-threaded database access

Adrian Casey caseyalice at adam.com.au
Wed Nov 24 09:22:09 CET 2010


I have a PyQt4 multi-threaded application which accesses many hosts
concurrently via ssh.  I would like each thread to have access to a
database so that it can look up details about the particular system it
is connected to.

The easy way is to have each thread create a connection to the database.
  However, this is wasteful and likely to exhaust the maximum number of
connections the database (postgresql) allows.  Instead, I would like to
share a single database connection between all threads.  In PyQt4, this
is not possible since the database connection can only be used by the
thread which created it.

So, I'm thinking I'll create a thread to just handle database queries.
The worker threads will communicate with the database thread using
signals and slots.

My question is, if I have 20 worker threads who have emitted a signal
which results in a database query, how do I ensure that the query
results are sent back to the originating thread?

Ideas, examples anyone?

Thanks.
Adrian.



More information about the Python-list mailing list