[PyQt] Blocking problems with httplib and QThread

parki parki.san at gmail.com
Sun Mar 1 00:31:03 GMT 2009


Hi guys,

I'm doing some testing of PyQT and HTTPLib and I stumbled upon a
(b)locking problem I can't get rid of.

Frist, I have the UI class, which basically sets up my GUI, has it's
own event loop and knows about my NetThread. I don't think the
implementation is relevant to get to the point so I'll just skip it.

Then there's NetThread which is basically a QThread with its own
separate event loop, a custom event to fire the connection code and a
timed data fetching mechanism. It looks something like...


class NetThread(QThread):
    def __init__(self,parent):
        QThread.__init__(self,parent)
        self.netTimer = QTimer()
        QObject.connect(
            self.netTimer,
            SIGNAL("timeout()"),
            self.updateData
        )

    def run(self):
        print "Producer starting"
        self.exec_()

    def customEvent(self, e):
        print "Custom event received!"
        self.connect()

    def updateData(self):
        # fetches information. i.e: httpresponse.read

    def connect(self):
        # connects to the server and...
        self.netTimer.start(2000)


My main function is as follows:


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow(MainWindow)
    ui.setupUi(MainWindow)
    producer = NetThread(ui)
    ui.addProducer(producer)
    MainWindow.show()
    app.exec_()

And the application is set up so that when a button is pressed on the
Main Window, the producer receives a custom event and the fetching
mechanism is fired.


My main concern is that whenever the producer thread is fetching
information from the net via httplib the whole application (UI) hangs
until that threads gets all the information back.
Am I doing something wrong and not really creating two different
threads with 2 event loops or do I really need to implement a
non-blocking HTTP client for my application?

Thanks :)



More information about the PyQt mailing list