#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    [RESOLVED] Thread not starting independently of the GUI


    Hi all,

    This is my problem. I have a small script that creates a GUI. However, when the GUI is created, a thread is supposed to be kicked off when it displays. This is not working as intended:

    Code:
    import gtk
    import pygtk
    
    import os
    import time
    import optparse
    import tempfile
    import threading
    
    SelectedValue = 0
    SelectedIter  = 0
    MainComboBox  = 0
    MainTextArea  = ""
    ExitSetting   = 0
    
    class ProcessingThread (threading.Thread):
      EntriesCount        = 0
      UpdatedFilesLines   = dict()
      InterleavedTempFile = ""
    
      def __init__(self):
        threading.Thread.__init__(self)
    
      def run(self):
        while ExitSetting == 0:
          print "Running!"
          time.sleep(1)
    
    class GUIFoo:
      SomeThread = 0
    
      def close_application(self, widget):
        ExitSetting = 1
    
        self.SomeThread.join()
    
        gtk.main_quit()
    
      def start_thread(self):
        self.SearcherThread = ProcessingThread()
        self.SearcherThread.run()
        self.SearcherThread.join()
    
      def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_resizable(True)  
        window.connect("destroy", self.close_application)
        window.set_title("GUI")
        window.set_border_width(5)
        window.set_size_request(1000, 400)
    
        .................................
    
        self.start_thread()
    
    def main():
      track = GUIFoo()
    
      # start the main GUI loop.
      gtk.main()
    
      return 0
    
    if __name__ == "__main__":
      main()
    I start my script and fully expect to see the running text being printed out by the thread and I see none. What am I missing?
    Last edited by elephantcoder; September 11th, 2015 at 10:09 AM.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,785
    Rep Power
    508
    I suggest that you remove the line with all the dots and then change occurrences of self.self. to self. .
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I suggest that you remove the line with all the dots and then change occurrences of self.self. to self. .
    Those are copy and paste errors. The dots are other code that is not relevant to the current problem. The self.self is a copy and paste error.

    I'll fix this once I figure out how to edit my original post.
  6. #4
  7. Not An Expert
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2015
    Posts
    404
    Rep Power
    3
    Originally Posted by elephantcoder
    I'll fix this once I figure out how to edit my original post.
    Due to your post count, our forums may be restricting your privileges for editing posts. I've upgraded your user permissions - so if you couldn't find an "edit post" button below your post before, it may be visible now.

    If you are still unable to edit your OP though, please email me at markr@imninjas.com

    Include the full edited text and I can make the edits for you.

    Thanks!

    Comments on this post

    • elephantcoder agrees : f
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0
    Originally Posted by markroberts
    Due to your post count, our forums may be restricting your privileges for editing posts. I've upgraded your user permissions - so if you couldn't find an "edit post" button below your post before, it may be visible now.

    If you are still unable to edit your OP though, please email me at markr@imninjas.com

    Include the full edited text and I can make the edits for you.

    Thanks!
    No, thank you!

    It works, very cool!
  10. #6
  11. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,785
    Rep Power
    508
    How do you expect the program to behave?
    ExitSetting needs to be global.
    Code:
      def close_application(self, widget):
        global ExitSetting
        ExitSetting = 1
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0
    Ok, after more research, it appears that using threads is a bad idea:
    https://docs.python.org/2/library/threading.html

    The main quote:
    CPython implementation detail: In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.
    Long story short, when your GUI is created the background thread will not run at all. On to multiprocessing module...

IMN logo majestic logo threadwatch logo seochat tools logo