pyqt一次打开多个网站

时间:2016-06-14 12:45:30

标签: python multithreading pyqt pyqt4 python-multiprocessing

我想用pyqt登录一些电子邮件地址,问题是我想同时登录它们,我知道可以通过多处理而不是多线程来完成({{3在python中的多线程将一个接一个地登录,我需要它立即启动所有网站并开始向它传递变量..我是Qt的初学者。我把它作为一个模型:

我试过的一个代码:

import sys
import urllib2
from PyQt4 import QtCore, QtGui
class SignalHelper(QtCore.QObject):
    data_downloaded = QtCore.pyqtSignal(object)
class DownloadThread(QtCore.QRunnable):
    def __init__(self, url):
        QtCore.QRunnable.__init__(self)
        self.url = url
        self._signal_helper = SignalHelper()
        self.data_downloaded = self._signal_helper.data_downloaded
    def run(self):
        print 'thread', QtCore.QThread.currentThread()
        info = urllib2.urlopen(self.url).info()
        self.data_downloaded.emit('%s\n%s' % (self.url, info))
class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.list_widget = QtGui.QListWidget()
        self.button = QtGui.QPushButton("Start")
        self.button.clicked.connect(self.start_download)
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.list_widget)
        self.setLayout(layout)
        ## Thread pool with maximum 5 simultaneously running threads
        self.thread_pool = QtCore.QThreadPool()
        self.thread_pool.setMaxThreadCount(5)
    def start_download(self):
        urls = ['https://mail.yahoo.com', 'https://mail.google.com', 'https://mail.yandex.com/', 'https://login.live.com', 'http://www.gmx.com/']
        for url in urls:
            downloader = DownloadThread(url)
            downloader.data_downloaded.connect(self.on_data_ready)
            self.thread_pool.start(downloader)
    def on_data_ready(self, data):
        self.list_widget.addItem(data)
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())

我想知道Qt是否启动了这些线程并且并行运行..不是一个接一个。有人可以举个例子吗? 我是如何用数据填充每个人的。

0 个答案:

没有答案