1

情况如下:我有一个加载特定 url 的类,还有一个参数列表,我需要发送到这个 url。我想使用线程“同时”加载这个 url,换句话说,不是加载一个 QWebView ,而是完成它而不是加载另一个我想一次打开 5 个窗口的窗口。

好的,所以现在的问题是,每个窗口都会有不同的速度,所以我需要跟踪正在使用的参数。

例如:

params = [1,2,3,4,5,6,7]
a = MyClass(1)
b = MyClass(2)
c = MyClass(3)

现在,如果假设 b 类在其他 2 个类之前完成,它将获得值 4,并将重新开始。

这是示例代码:

#! /usr/bin/env python2.7

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import sys, signal

class Grabber(QWebView):
    def __init__(self, param=None):
        QWebView.__init__(self)
        self.loadFinished.connect(self._loadComplete)
        url = QUrl('http://some website.com/search?param=%s'%param)
        self.load(url)
        self.show()

    def _loadComplete(self):
        print "Done"

if __name__ == "__main__":
    app = QApplication(sys.argv)
    # Dont know what to do with the next 2 lines
    thread_pool = QThreadPool()
    thread_pool.setMaxThreadCount(10)

    param = [1,2,3,4,5,6,7,8,9,10,11,12] # and so on

    # Whant to achive something similar:
    for i in param:
        a = Grabber(i)
        b = Grabber(i)
        c = Grabber(i)
        d = Grabber(i)
        e = Grabber(i)

    if signal.signal(signal.SIGINT, signal.SIG_DFL):
        sys.exit(app.exec_())
    app.exec_() 

我很确定我需要使用线程、ether python native 线程或 PyQt4 QThread。

4

1 回答 1

1

看看这样的事情是否有效。该类bookie所做的是将调用的次数存储loadFinished到变量count中,并返回列表中的相应项params。创建grabber对象时,它通过 将其映射到一个数字QSignalMapper,因此您可以通过调用来检索信号的发送者mapper.mapping(id-of-grabber-object)。您可以继续尝试此代码,看看它是否适合您,然后再修改您的代码:

#!/usr/bin/env python
from PyQt4.QtCore import pyqtSlot, QObject, QSignalMapper, QTimer
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView


class bookie(QObject):
    def __init__(self, parent=None):
        super(bookie, self).__init__(parent)

        self.count  = 0
        self.url    = 'http://some website.com/search?param={0}'
        self.params = range(100) # instead of [1,2,3,4,5,6,7,8,9,10,11,12] and so on...

        Grabber = QWebView # This would be your Grabber class

        self.mapper = QSignalMapper(self)
        self.mapper.mapped.connect(self.on_mapper_mapped)

        for grabberNumber in range(10): # Create 10 Grabber instances
            grabber = Grabber()
            grabber.loadFinished.connect(self.mapper.map)

            self.mapper.setMapping(grabber, grabberNumber)

            grabber.loadFinished.emit(True) # Initialize the grabber by emitting loadFinished

    def on_mapper_mapped(self, gNumber):
        self.count += 1
        if self.count < len(self.params):
            gParam  = self.params[self.count]   
            grabber = self.mapper.mapping(gNumber)
            #grabber.load(QUrl(self.url.format(gParam)))

            # Next 2 lines for testing purposes, remove & uncomment the previous line

            print "GRABBER:", gNumber, "PARAMETER:", gParam
            QTimer.singleShot(1, lambda:grabber.loadFinished.emit(True)) 

if __name__ == "__main__":
    import  sys

    app = QApplication(sys.argv)
    main = bookie()
于 2012-12-19T12:50:58.470 回答