Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81662

Re: Fwd: Python Signal/Slot + QThred code analysis

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <torriem+gmail@torriefamily.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'subject: + ': 0.07; 'subject:code': 0.07; 'sys': 0.07; '"__main__":': 0.09; '%s"': 0.09; '*args,': 0.09; '__name__': 0.09; 'callback': 0.09; 'grid': 0.09; 'def': 0.12; 'gui': 0.12; 'random': 0.14; 'thread': 0.14; '**kwargs)': 0.16; '**kwargs):': 0.16; 'bind': 0.16; 'blocks': 0.16; 'emit': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'invokes': 0.16; 'scope.': 0.16; 'subject:analysis': 0.16; 'prevent': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'users.': 0.18; 'variable': 0.18; 'app': 0.19; 'properly': 0.19; 'import': 0.22; 'putting': 0.22; 'print': 0.22; 'load': 0.23; 'header:User- Agent:1': 0.23; 'exists': 0.24; 'mind.': 0.24; 'sends': 0.24; 'posts': 0.26; 'pass': 0.26; 'post': 0.26; 'subject:/': 0.26; 'defined': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; 'code': 0.31; 'skip:q 20': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'skip:_ 10': 0.34; 'skip:u 20': 0.35; 'done.': 0.35; 'but': 0.35; 'data,': 0.36; 'minutes,': 0.36; 'right?': 0.36; 'being': 0.38; 'christian': 0.38; 'sometimes': 0.38; 'message-id:@gmail.com': 0.38; 'skip:m 40': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'resource': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'users': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'signal': 0.60; 'skip:t 30': 0.61; 'new': 0.61; 'matter': 0.61; 'subject:Fwd': 0.61; 'more': 0.64; 'fire': 0.65; 'url:user': 0.65; 'here': 0.66; 'invalid': 0.68; 'skip:w 30': 0.69; 'risk': 0.72; 'asynchronous': 0.84; 'leak.': 0.84; 'trades': 0.84; 'lazy': 0.91
X-Virus-Scanned amavisd-new at torriefamily.org
Date Thu, 27 Nov 2014 21:06:38 -0700
From Michael Torrie <torriem@gmail.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131118 Thunderbird/17.0.11
MIME-Version 1.0
To python-list@python.org
Subject Re: Fwd: Python Signal/Slot + QThred code analysis
References <CAAp0bGttki1B=1q5SX_C39L_dRB22YOQiTbVxdvo4jSee0srcw@mail.gmail.com> <CAAp0bGsgMTK7MS1Wvh13304et7Bb7XUyfZTU8ZmF2++BuLj7dw@mail.gmail.com> <5473E188.5020608@gmail.com> <CAAp0bGtYCEWNLV7tnR6O5N706_3AkO9XRBs1pNP8TCxfctUyow@mail.gmail.com> <5474A333.2090207@mrabarnett.plus.com> <CAAp0bGsGLsWoXQnqFYN3KLeUsmcD5ujF80sasM16wx70zQ1xUA@mail.gmail.com> <CAAp0bGvhhWcLO76UQ+nXZuhazjJr9wsjwBkuCX78O4K=em9wuQ@mail.gmail.com> <5474C889.5090503@mrabarnett.plus.com> <CAAp0bGtv13QaPhfTx+tzSe6ZpQ9RCLiWrG1Cv+qfEw3JZ41o9g@mail.gmail.com> <547545CF.60208@gmail.com> <CAAp0bGuMRmEXXFpHaVx0MBoQ72N3MYXYwnnxpS58w-LSV6vTdw@mail.gmail.com> <5476A134.7050407@gmail.com> <5476A48C.1050607@gmail.com> <CAAp0bGtirGVFRAdE3-eosTVfwTAeLO0-XcPyY7+JVSsujqs7bg@mail.gmail.com> <5477AB1E.3030609@gmail.com> <CAAp0bGvR+AUiedDdw6KLTmBjJ-PSKvxEnwCxbRj7xB7isFEvMg@mail.gmail.com> <CAAp0bGvypnRutp5MmRV6sOu2iU267TbB-yeyteUN9kH6twVKKw@mail.gmail.com>
In-Reply-To <CAAp0bGvypnRutp5MmRV6sOu2iU267TbB-yeyteUN9kH6twVKKw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.16390.1417147615.18130.python-list@python.org> (permalink)
Lines 106
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417147615 news.xs4all.nl 2851 [2001:888:2000:d::a6]:42301
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81662

Show key headers only | View raw


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
        self.start()

    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)
	uifile.open(QFile.ReadOnly)
	ui = loader.load(uifile)
	uifile.close()
	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()

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

        grid.addWidget(nickname)
        box.setLayout(grid)

        # Access global variable MainWindow, defined below
        MainWindow.vlay.addWidget(box)

        # clean up the worker to prevent resource leak.
        worker.quit()
        worker.wait()

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

    app = QApplication(sys.argv)

    MainWindow = loadui("main.ui")
    MainWindow.vlay.setAlignment(Qt.AlignTop)
    timer = QTimer()
    timer.start(5000)
    timer.timeout.connect(output_slot)
    id64 = '123'

    MainWindow.show()
    app.exec_()

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Fwd: Python Signal/Slot + QThred code analysis Michael Torrie <torriem@gmail.com> - 2014-11-27 21:06 -0700

csiph-web