0

编辑:

为了您的方便,我准备了这个SSCCE :(只需运行它)

from PyQt4 import QtCore, QtGui, QtNetwork
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply
import time
import sys
import json

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

class SSCCE(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 578)
        QMetaObject.connectSlotsByName(MainWindow)

    def main(self):
        request = QNetworkRequest()
        request.setUrl(QUrl("http://search.twitter.com/search.json?q=blue&rpp=1"))

        self.network_manager = QNetworkAccessManager()
        self.network_manager.finished.connect(self._request_finished)        

        self.network_manager.post(request, self.urlencode_post({'a' : 'b'}))

    def urlencode_post(self, data):
        post_params = QUrl()
        for (key, value) in data.items():
            post_params.addQueryItem(key, unicode(value))            

        return post_params.encodedQuery()

    def func2(self):
        request = QNetworkRequest()
        request.setUrl(QUrl("http://search.twitter.com/search.json?q=red&rpp=1"))
        request.setRawHeader('content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
        self.network_manager.finished.connect(self.func3)
        self.network_manager.get(request)

    def func3(self, reply):
        if not reply.error() == QNetworkReply.NoError:
            # request probably failed
            print(reply.error())
            print(reply.errorString())
        else:
            reply.close()

    def func1(self, reply):
        ans = reply.readAll()
        print(ans)
        time.sleep(5)
        print("""\n 
IT PRINTS THEM BOTH:
===================
%s
"""%ans)
        self.func2()

    def _request_finished(self, reply):
        if not reply.error() == QNetworkReply.NoError:
            # request probably failed
            print(reply.error())
            print(reply.errorString())
        else:
            reply.close()
            self.func1(reply)


app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = SSCCE()
ui.setupUi(MainWindow)
ui.main()

MainWindow.show()
sys.exit(app.exec_())


原始问题:

我的代码:(相关部分)

    def update(self):
        request = QNetworkRequest()
        request.setUrl(QUrl("someurl"))
        self.network_manager.finished.connect(self._update)
        self.network_manager.get(request)

    def _update(self, reply): # update stage 2    
        if not reply.error() == QNetworkReply.NoError:
            # request probably failed
            print(reply.error())
            print(reply.errorString())
            print("retrying")
            self.update()
        else:
            reply.abort()
            #print(str(reply.readAll().data()))
            data = json.loads(str(reply.readAll().data())) # get data
            #work with the data (irrelevant)

    def sendBearer_req(self):

        request = QNetworkRequest()
        request.setUrl(QUrl("someotherurl"))

        self.network_manager = QNetworkAccessManager()
        self.network_manager.finished.connect(self._request_finished)        

        self.network_manager.post(request, self.urlencode_post({'some' : 'thing'}))

    def sendBearer(self, reply):
        reply.abort()
        ans = reply.readAll()        
        print(ans)
        time.sleep(5)
        print(ans)

        try:
            self.bearer = json.loads(str(ans))                
            self.update()            
        except:
            raise #for debugging
            self.sendBearer_req() #retry        

    def _request_finished(self, reply):
        if not reply.error() == QNetworkReply.NoError:
            # request probably failed
            print(reply.error())
            print(reply.errorString())
            print("retrying")
            self.sendBearer_req()
        else:
            self.sendBearer(reply)

问题:

在这部分:

reply.abort()
ans = reply.readAll()        
print(ans)
time.sleep(5)
print(ans)

我很确定abort它不起作用,因为print它首先打印出我想要的东西,但是在它等待时,显然它继续执行代码并进入update发送另一个请求的函数。因此,在第二个print中,两个回复混合在一起并打印两者。

(顺便说一句,我也试过close- 结果相同)

我究竟做错了什么?

4

1 回答 1

2

我发现了问题!

问题是我没有断开网络管理器finished信号与它所连接的前一个功能的连接,因此导致回复转到两个功能(新连接和旧连接)

.disconnect()在再次连接之前使用命令解决了它:

self.network_manager.finished.disconnect() #this!!!
self.network_manager.finished.connect(self._update)

在 SSCCE 中:

self.network_manager.finished.disconnect() #this!!!
self.network_manager.finished.connect(self.func3)

希望我能帮助你未来的观众!

于 2013-05-03T18:44:24.797 回答