Important: Please read the Qt Code of Conduct -

User interface freezed when using concurrent.futures.ThreadPoolExecutor

  • I think the user interface shouldn't be freezed when using concurrent.futures.ThreadPoolExecutor here, but it doesn't meet my expectations,anyone can explain why ? any other solutions here to not let user interface freezed?

    import sys   
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    import time
    import concurrent.futures
    import urllib.request
    URLS = ['',
    # Retrieve a single page and report the url and contents
    def load_url(url, timeout):
        conn = urllib.request.urlopen(url, timeout=timeout)
        return conn.readall()
    class MainWindow(QMainWindow):
        def __init__(self):
            QTimer.singleShot(3000, self.speedCalculate)
    #        self.timerC = QTimer();
    #        self.timerC.timeout.connect(self.speedCalculate)
    #        self.timerC.start(1000)
        def speedCalculate(self):#compare with for loop
            # We can use a with statement to ensure threads are cleaned up promptly
            with concurrent.futures.ThreadPoolExecutor(max_workers=len(URLS)) as executor:
                # Start the load operations and mark each future with its URL
                future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
                for future in concurrent.futures.as_completed(future_to_url):
                    url = future_to_url[future]
                        data = future.result()
                    except Exception as exc:
                        print('%r generated an exception: %s' % (url, exc))
                        print('%r page is %d bytes' % (url, len(data)))
            print('t2-t1-------------', t2-t1)
    #        for url in URLS:
    #            data=load_url(url, 60)
    #            print(url, len(data))
    #        t3=time.clock()
    #        print('t3-t2-------------', t3-t2)
    if __name__ == '__main__':                         
        app =QApplication(sys.argv)      
        splitter =MainWindow()        


Log in to reply