Results 1 to 2 of 2

Thread: Application sometimes crashes, other times does a task incorrectly (Multithreading)

  1. #1
    Join Date
    Mar 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Application sometimes crashes, other times does a task incorrectly (Multithreading)

    Hi,

    Apologies the code is in Python.

    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:

    Qt Code:
    1. class Functions:
    2.  
    3. solveComputationSignal = Signal(str)
    4. updateStatusSignal = Signal(int, str)
    5. text = None
    6.  
    7. @classmethod
    8. def setResultText(self, text):
    9. self.text = text
    10.  
    11. @classmethod
    12. def solveComputation(cls, platform, computation, param=None):
    13. #Not the entirety of the method is listed here
    14. result = urllib.urlopen(COMPUTATION_URL).read()
    15. if param is None:
    16. cls.solveComputationSignal.emit(result)
    17. else:
    18. cls.solveAlternateComputation(platform, computation)
    19.  
    20. while not self.text:
    21. time.sleep(3)
    22.  
    23. return self.text if self.text else False
    24.  
    25.  
    26.  
    27. @classmethod
    28. def updateCurrentStatus(cls, platform, statusText):
    29. cls.updateStatusSignal.emit(platform, statusText)
    To copy to clipboard, switch view to plain text mode 
    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):

    Qt Code:
    1. class WorkerPlatform1(QThread):
    2.  
    3. #Init and other methods are here
    4.  
    5. def run(self):
    6.  
    7. #Thread does its job here, but then when it needs to present the
    8. #computation, instead of emitting a signal, this is what I do
    9.  
    10. self.f = functions.Functions
    11.  
    12. result = self.f.solveComputation(platform, computation)
    13.  
    14. if result:
    15. #Go on with the task
    16. else:
    17. self.f.updateCurrentStatus(platform, "Error grabbing computation!")
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. 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)
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Application sometimes crashes, other times does a task incorrectly (Multithreadin

    Quote Originally Posted by Deusdies View Post
    Hi,

    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.
    and what about us?

    (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 obvious way is to use a QDialog...

    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?
    Yes.

    both of these methods in the Functions class are accessed by many threads almost simultaneously. Is this even advisable
    If there is only one Functions instance per thread, then it is ok. If you are sharing the instance, then no. Since these are classmethods then it is not thread safe.

    E.G
    there should be no need for
    Qt Code:
    1. while not self.text:
    2. time.sleep(3)
    3.  
    4. return self.text if self.text else False # If you really need this x if x else y, then you are using this method with threads in a non-thread safe way.
    5. # The 'while not self.text' should guarantee that self.text is not empty/null. If it is not, then you must have another thread that is somehow interfering.
    To copy to clipboard, switch view to plain text mode 

    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?
    Yes.

    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?
    Perhaps that will work. It is not a good solution, though - make you Functions methods into instance methods, and use one Functions instance per thread.


    oh, and since you neglected to do so, here is the SO thread that your also started:
    http://stackoverflow.com/questions/1...m-within-the-q
    Last edited by amleto; 3rd December 2012 at 10:29.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Hiding any running Application icon on windows task bar
    By nagabathula in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2012, 07:11
  2. Multithreading application and GUI
    By Luc4 in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2010, 11:18
  3. Replies: 2
    Last Post: 11th February 2010, 09:35
  4. Task to another application
    By zlosynus in forum Qt Programming
    Replies: 0
    Last Post: 6th August 2008, 10:04
  5. How to hide application button on the task bar?
    By QCasper in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2008, 23:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.