PyQt4中QWebView的多线程 [英] multiple threads of QWebView in PyQt4
问题描述
情况如下:我有一个加载特定 url 的类,还有一个参数列表,我需要发送到这个 url.我想使用线程来同时"加载这个 url,换句话说,不是加载一个 QWebView ,而不是加载另一个我想一次打开 5 个窗口的另一个.
Here is the situation: I have a class that loads specific url, also have a list of parameters, that I need to send to this url. I want to use threads to load this url kinda 'simultaneously', in other words in stead of loading one QWebView , than finish it than load another one I want to open 5 windows at a time.
好的,那么现在的问题是,每个窗口的速度都不同,所以我需要跟踪正在使用的参数.
OK, So now the problem is, every single window will have different speed, so I need to keep track of what parametrs are in use.
例如:
params = [1,2,3,4,5,6,7]
a = MyClass(1)
b = MyClass(2)
c = MyClass(3)
现在,如果假设班级 b 在其他 2 个班级之前完成,它将获得值 4,并将重新开始.
now if lets say class b has finished befoure other 2 classes, it will get value 4, and will start it again.
这是示例代码:
#! /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 本地线程或 PyQt4 QThread.
I'm pretty sure I need to use threads, ether python native threads or PyQt4 QThread.
推荐答案
看看这样的事情是否有效.这个bookie
类所做的就是将loadFinished
被调用的次数存入变量count
中,并返回列表中对应的项参数
.在创建grabber
对象时,它通过QSignalMapper
将其映射到一个数字,因此您可以通过调用mapper.mapping(id-of-grabber-object)
.在修改您的代码之前,您可以继续尝试此代码,看看它是否适合您:
See if something like this works. What this bookie
class does is store the number of times loadFinished
is called into the variable count
, and returns the corresponding item in the list params
. When creating the grabber
objects, it maps it to a number through QSignalMapper
, so you may retrieve the sender of the signal by calling mapper.mapping(id-of-grabber-object)
. You can go ahead and try this code to see if it works for you before modifying yours:
#!/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()
这篇关于PyQt4中QWebView的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!