简体   繁体   English

PyQt4,防止app.exec_()挂起主线程

[英]PyQt4, prevent app.exec_() from hanging the main thread

I have some simple code that loads google.com with the PyQt4 library. 我有一些简单的代码,可通过PyQt4库加载google.com。
This is the code: 这是代码:

import sys
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebView

class Browser(QWebView):
    def __init__(self):
        QWebView.__init__(self)
        self.loadFinished.connect(self._result_available)

    def _result_available(self, ok):
        frame = self.page().mainFrame()
        #print(frame.toHtml())
        app.exit()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = Browser()
    view.load(QUrl('http://www.google.com'))
    print('start')
    app.exec_()#hangs the main thread
    print('end')

My problem with this code is that app.exec_() hangs the main thread for a while, between the print start and print end. 我的这段代码的问题是app.exec_()在打印开始和打印结束之间将主线程挂了一段时间。
Is there a way to scrape a website in PyQt4 without making the main thread hang for a while. 有没有一种方法可以在不使主线程挂起一会儿的情况下在PyQt4中抓取网站。
I would like to resume the main thread's normal execution of code after app.exec_() . 我想在app.exec_()之后恢复主线程的代码正常执行。

It's probably just taking time to download the content from the web. 从网上下载内容可能只是花时间。 app.exec_() is what runs your application basically. app.exec_()基本上可以运行您的应用程序。 So if anything is indeed hanging it's actually some other factor that influences the proper execution of your application. 因此,如果确实有任何问题挂起,实际上是其他一些因素会影响应用程序的正确执行。 Any statements that are after the app.exec_() will be executed once you close the application. 关闭应用程序后,将执行app.exec_()之后的所有语句。 Usually app.exec_() is called at the end of the main (in C++ you do return app.exec_() which you can also do in PyQt of course). 通常, app.exec_()main的末尾调用(在C ++中,您确实return app.exec_() ,当然您也可以在PyQt中这样做)。

When working with content that requires time to be downloaded and displayed in the UI, you have to add multithreading in order to allow the main thread to continue work properly (thus not creating the so called UI freeze ) but at the same time do some work in the background. 当处理需要下载并显示在UI中的时间的内容时,您必须添加多线程以使主线程能够继续正常工作(因此不创建所谓的UI Frozen ),但同时需要做一些工作在后台。 The sole purpose of the main thread is to keep track of the UI and run it. 主线程的唯一目的是跟踪UI并运行它。 Anything else that you add to the main thread will prevent the UI from working in a fluent manner. 您添加到主线程中的任何其他内容都将阻止UI流畅地工作。 You have several options here - inheriting QThread (I wouldn't suggest doing that for this scenario in particular), using QThread + QObject in order to incorporate slots and signal in your application (for example: page downloaded? -> if yes, signal the UI to display the content), QRunnable etc. 您在这里有几种选择-继承QThread (特别是在这种情况下,我不建议这样做),使用QThread + QObject以便在应用程序中合并插槽和信号(例如:页面下载?->如果是,则信号UI以显示内容), QRunnable等。

So my advice is to download the content in a separate thread and once it's done you can add it to your UI as well as provide some other form of visual feedback (this also includes print statements :P). 因此,我的建议是在单独的线程中下载内容,完成后,您可以将其添加到您的UI中,并提供其他形式的视觉反馈(这也包括print语句:P)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM