Python QT QNetworkRequest exits with “(process:326

2019-06-08 03:12发布

I have a python qt program which downloads many urls from a server. Most of the urls are small images (icons) and are unique.

For some strange reason after the requests have finished python/qt still has an active http connections to the server. And in my case this means many http connections.

The QT documentation states that the QNetworkAccessManager - "6 requests are executed in parallel for one host/port combination." How can so many, hundreds, of http connections be kept open?

After a few minutes the connections are closed, but if the application downloads too many too quickly the application dies with "(process:3265): GLib-ERROR **: Creating pipes for GWakeup: Too many open files"

In the finished() signal I am calling reply.deleteLater() then ensuring my application has no reference to the reply.

Firstly can I catch the "Too many open files" error and handle it correctly?

Secondly can I prevent so many http connections being left open before they are closed?

---- Snippets ----

image_request = Qt.QNetworkRequest()
image_request.setUrl(Qt.QUrl(url))
self.image_reply = self.manager.get(image_request)
self.image_reply.finished.connect(self.image_available)

image_available is

def image_available(self):
    if self.image_reply.error() == Qt.QNetworkReply.NoError:
        data = self.image_reply.readAll()
        img = Qt.QImage()
        img.loadFromData(data)
        self.lbl_icon.setPixmap(Qt.QPixmap(img))
        self.image_reply.deleteLater()
        self.image_reply = None

2条回答
叛逆
2楼-- · 2019-06-08 03:48

I discovered the problem after lots of debugging.

It turns out that the self.manager was getting overwritten quite a lot (accidentally it should have been created and assigned once.

self.manager = Qt.QNetworkAccessManager(self)

was occurring more than once and it shouldnt.

Now there is really once QNetworkAccessManager there are only 6 http connections + 6 https connections which exactly what I wanted. :)

查看更多
孤傲高冷的网名
3楼-- · 2019-06-08 04:01

Is your condition self.image_reply.error() == Qt.QNetworkReply.NoError always true? If it ts false, deleteLater will not be called.

Try to set 'Connection: close' header:

image_request.setRawHeader("Connection", "close");

Also try to call self.image_reply.close() manually.

查看更多
登录 后发表回答