Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8946815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:35:48+00:00 2026-06-15T12:35:48+00:00

I have an application that has a GUI thread and many different worker threads.

  • 0

I have an application that has a GUI thread and many different worker threads. In this application, I have a functions.py module, which contains a lot of different “utility” functions that are used all over the application.

Yesterday the application has been released and some users (a minority, but still) has reported problems with the application crashing. I looked over my code and noticed a possible design flaw, and would like to check with the lovely people of SO and see if I am right and if this is indeed a flaw.

Suppose I have this defined in my functions.py module:

class Functions:

    solveComputationSignal = Signal(str)
    updateStatusSignal = Signal(int, str)
    text = None

    @classmethod
    def setResultText(self, text):
        self.text = text

    @classmethod
    def solveComputation(cls, platform, computation, param=None):
        #Not the entirety of the method is listed here       
        result = urllib.urlopen(COMPUTATION_URL).read()
        if param is None:
            cls.solveComputationSignal.emit(result)
        else:
            cls.solveAlternateComputation(platform, computation)

        while not self.text:
            time.sleep(3)

        return self.text if self.text else False



    @classmethod
    def updateCurrentStatus(cls, platform, statusText):
        cls.updateStatusSignal.emit(platform, statusText)

I think these methods in themselves are fine. The two signals defined here are connected to in the GUI thread. The first signal pops-up a dialog in which the computation is presented. The GUI thread calls the setResultText() method and sets the resulting string as entered by the user (if anyone knows of a better way to wait until the user has inputted the text other than sleeping and waiting for self.text to become True, please let me know). The solveAlternateComputation is another method in the same class that solves the computation automatically, however, it too calls the setResultText() method that sets the resulting text.

The second signal updates the statusBar text of the main GUI as well.

What’s worse is that I think the above design, while perhaps flawed, is not the problem.

The problem lies, I believe, in the way I call these methods, whihch is from the worker threads (note that I have multiple similar workers, all of which are different “platforms”)

Assume I have this (and I do):

class WorkerPlatform1(QThread):

    #Init and other methods are here

    def run(self):

        #Thread does its job here, but then when it needs to present the  
        #computation, instead of emitting a signal, this is what I do

        self.f = functions.Functions

        result = self.f.solveComputation(platform, computation)

        if result:
            #Go on with the task
        else:
            self.f.updateCurrentStatus(platform, "Error grabbing computation!")

In this case I think that my flaw is that the thread itself is not emitting any signals, but rather calling callables residing outside of that thread directly. Am I right in thinking that this could cause my application to crash? Although the faulty module is reported as QtGui4.dll

One more thing: both of these methods in the Functions class are accessed by many threads almost simultaneously. Is this even advisable – have methods residing outside of a thread be accessed by many threads all at the same time? Can it so happen that I “confuse” my program? The reason I am asking is because people who say that the application is not crashing report that, very often, the solveComputation() returns the incorrect text – not all the time, but very often. Since that COMPUTATION_URL‘s server can take some time to respond (even 10+ seconds), is it possible that, once a thread calls that method, while the urllib library is still waiting for server response, in that time another thread can call it, causing it to use a different COMPUTATION_URL, which will result in it returning an incorrect value on some cases?

Finally, I am thinking of solutions: for my first (crashing) problem, do you think the proper solution would be to directly emit a Signal from the thread itself, and then connect it in the GUI thread? Is that the right way to go about it?

Secondly, for the solveComputation returning incorrect values, would I solve it by moving that method (and accompanying methods) to every Worker class? then I could call them directly and hopefully have the correct response – or, dozens of different responses (since I have that many threads) – for every thread?

Thank you all and I apologize for the wall of text.

EDIT: I would like to add that when running in console with some users, this error appears QObject: Cannot create children for a parent that is in a different thread.
(Parent is QLabel(0x4795500), parent's thread is QThread(0x2d3fd90), current thread is WordPressCreator(0x49f0548)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:35:49+00:00Added an answer on June 15, 2026 at 12:35 pm

    Your design is flawed if you really are using your Functions class like this with classmethods storing results on class attributes, being shared amongst multiple workers. It should be using all instance methods, and each thread should be using an instance of this class:

    class Functions(QObject):
    
        solveComputationSignal = pyqtSignal(str)
        updateStatusSignal = pyqtSignal(int, str)
    
        def __init__(self, parent=None):
            super(Functions, self).__init__(parent)
            self.text = ""
    
        def setResultText(self, text):
            self.text = text
    
    
        def solveComputation(self, platform, computation, param=None):
            result = urllib.urlopen(COMPUTATION_URL).read()
            if param is None:
                self.solveComputationSignal.emit(result)
            else:
                self.solveAlternateComputation(platform, computation)
    
            while not self.text:
                time.sleep(3)
    
            return self.text if self.text else False
    
    
        def updateCurrentStatus(self, platform, statusText):
            self.updateStatusSignal.emit(platform, statusText)
    
    
    # worker_A
        def run(self):
            ...
            f = Functions()
    # worker_B
        def run(self):
            ...
            f = Functions()
    

    Also, for doing your urlopen, instead of doing sleeps to check for when it is ready, you can make use of the QNetworkAccessManager to make your requests and use signals to be notified when results are ready.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that has two threads. The first one (the main thread)
I have a Qt application that has two threads: the main thread that handles
I have a GUI C# application that has a single button Start/Stop. Originally this
Suppose I have a GUI application which has a background thread which runs for
I have an application that has 2 beans with the same name, but which
I have a Win32 GUI application that has several edit controls (plain old EDIT
I have a Windows program that has a GUI which also uses a command
I have an application that has a GUI and TCP server. The TCP server
I have a application that has a GUI where you can do some settings.
We have an application that has a database full of polygons (currently stored as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.