Fwd: Python Signal/Slot + QThred code analysis

Michael Torrie torriem at gmail.com
Fri Nov 28 05:06:38 CET 2014

On 11/27/2014 04:29 PM, Juan Christian wrote:
> Is that right? But I don't think that always calling the site this way is
> good for them and for me, sometimes I may not get new users for like 10~15
> minutes, so I would have wasted a lot of calls for nothing. That's why I
> only instantiate/call the site when I get new users.

Okay, here's a reworking of the code that invokes a new QThread instance
each time.  Note the QThread instance has to be bound to the MainWindow
so that it won't be destroyed when it goes out of scope.  Also the
Worker thread sends a signal with the data, so there's no need to check
worker.trades and risk it being invalid data.

Perhaps this would be more what you had in mind.

You may want to look into asynchronous I/O as well.  That does not
require threads at all.  Fire off a request to load a url, and then get
a callback when it's done.

Anyway, here's the code (too lazy to post it to pastebin):

import time
import random
from PySide.QtCore import QObject, QThread, Signal, QFile, QTimer, Qt
from PySide.QtGui import QGroupBox, QGridLayout, QTextEdit, QApplication
from PySide.QtUiTools import QUiLoader

class Worker(QThread):

    # This is the signal that we'll emit every time
    # we load a new set of values from the url
    new_value = Signal(QThread, str,str)

    def __init__(self, url, *args, **kwargs):
        super(Worker, self).__init__(*args, **kwargs)
        self.url = url

    def run(self):
        # Doesn't matter how long this takes because it only
        # blocks the thread's event loop, not the GUI
        print ('Starting work...')
        time.sleep(random.random() * 2)
        trades = str(random.random() * 25)
        posts = str(random.random() * 100)
        print ('Finished work.')

        # communicate the values via the signal
        # we have to pass self so that the callback
        # can properly clean up after the thread.
        self.new_value.emit(self, trades, posts)

def loadui(file_name):
	loader = QUiLoader()
	uifile = QFile(file_name)
	ui = loader.load(uifile)
	return ui

if __name__ == "__main__":
    import sys

    # Putting this in here because it requires access to MainWindow
    # which only exists if this part of the code runs.
    def create_topic(worker, trades, posts):
        box = QGroupBox()
        grid = QGridLayout()
        nickname = QTextEdit()

        nickname.setText("%s : %s" % (trades, posts))


        # Access global variable MainWindow, defined below

        # clean up the worker to prevent resource leak.

    def output_slot():
        # in order for the thread to stay running, we have to bind it to
        # Main Window, which we
        worker = Worker('http://www.tf2outpost.com/user /' + id64,

    app = QApplication(sys.argv)

    MainWindow = loadui("main.ui")
    timer = QTimer()
    id64 = '123'


More information about the Python-list mailing list