Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: [SOLVED] Pygtk Threads Problem

  1. #1
    Join Date
    Nov 2008
    Location
    Denver, UK
    Beans
    271
    Distro
    Ubuntu 9.10 Karmic Koala

    [SOLVED] Pygtk Threads Problem

    I have the following code dealing with pygtk threads http://pastebin.com/f416ceb0c, if line 46 is commented out it works as expected but with line 46 the gui blocks until the time is expired and than it pulses my progress bar, I'm new to thread programming in Python and would appreciate any help.

  2. #2
    Join Date
    Sep 2005
    Beans
    143
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: Pygtk Threads Problem

    Hi,

    The main issue that you are having revolves around the nature of the gtk. The gtk runs in one thread so updates to the gui and callback functions happen serially. All calls that update a gui object are actually queued and executed by the gtk thread when it is able. Thus, the time.sleep call causes the gtk to sleep and not update the gui. Does my explanation make sense?

  3. #3
    Join Date
    Nov 2008
    Location
    Denver, UK
    Beans
    271
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Pygtk Threads Problem

    It does, but how can I perform a long task while pulsing progress bar so that the user knows something is happening? Both tasks can't be in the same thread??

  4. #4
    Join Date
    Sep 2005
    Beans
    143
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: Pygtk Threads Problem

    The long task needs to be in a separate thread. Create another thread in the callback to do the task. Once that thread concludes have it notify the other thread that the task is finished. Alternatively, use one thread to perform the task and update the progress bar as it goes.

  5. #5
    Join Date
    Sep 2005
    Beans
    143
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: Pygtk Threads Problem

    also, the following code will allow for gtk events to be processed:

    while gtk.events_pending():
    gtk.main_iteration(False)


    Although, that strategy has been hit or miss for me. I found that bit of code on the pygtk faq page, it might have some helpful insights for you.

  6. #6
    Join Date
    Nov 2008
    Location
    Denver, UK
    Beans
    271
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Pygtk Threads Problem

    Is there any standard way of notifying the main thread that the child thread has completed or do I need to brew my own solution?

  7. #7
    Join Date
    Sep 2005
    Beans
    143
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: Pygtk Threads Problem

    I'm not sure there is a standard way - I'm fairly new to python threading myself. I think you'll just have to invent that wheel yourself.

  8. #8
    Join Date
    Nov 2008
    Location
    Denver, UK
    Beans
    271
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Pygtk Threads Problem

    This is what I came up with, maybe not the most elegant, but it works.

    Code:
    import gobject
    import threading
    import random, time
    import gtk
     
    class FractionSetter(threading.Thread):
    	def __init__ (self, parent):
    		threading.Thread.__init__(self)
    		self.parent = parent
     
    	def run(self):
    		time.sleep(5)
    		self.parent.quit_updating()
     
    	def stop(self):
    		self = None
     
    class ThreadTest:
    	def __init__(self):
    		gtk.gdk.threads_init()
    		self.fs = None
    		self.worker_processing = False
     
    	def quit_updating(self):
    		self.worker_processing = False
     
    	def main_quit(self, obj):
    		if not self.fs == None:
    			self.fs.stop()
     
    		gtk.main_quit()
     
    	def pulse(self):
    		self.progressbar.pulse()
     
    		return self.worker_processing
     
    	def start_new_thread (self, button, data=None):
    		if self.fs == None:
    			self.fs = FractionSetter(self)
     
    		self.fs.start()
     
    		self.worker_processing = True
    		gobject.timeout_add(100, self.pulse)
     
    	def main(self):
    		window = gtk.Window()
    		vbox = gtk.VBox()
    		self.progressbar = gtk.ProgressBar()
    		button = gtk.Button("Start")
    		vbox.pack_start(button, True, True)
    		vbox.pack_start(self.progressbar, True, True)
    		window.add(vbox)
    		window.show_all()
    		button.connect("clicked", self.start_new_thread)
    		window.connect('destroy', self.main_quit)
     
    		gtk.gdk.threads_enter()
    		gtk.main()
    		gtk.gdk.threads_leave()
     
    if __name__ == '__main__':
    	tt = ThreadTest()
    	tt.main()

  9. #9
    Join Date
    Nov 2008
    Location
    Denver, UK
    Beans
    271
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Pygtk Threads Problem

    Does anyone know how to kill a child process before it is completed?

  10. #10
    Join Date
    Aug 2008
    Location
    usa
    Beans
    45
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Pygtk Threads Problem

    I hope this is not an intrusion, but this thread applies pretty directly to a problem I have. I have put together a simple nautilus python extension to show the total disk space used per folder, recursively, in the Nautilus listview. It works and it helps me a great deal, but the first time I click on a large folder (like root!) it blocks nautilus for 2-3 seconds, long enough for it to turn dark, then updates the display.

    I suspect there is no good way for a nautilus extension to handle this? Or is there? I have some python and pygtk knowledge, but none on threading.

    It uses os.walk.

    Incidentally, it helped me find 50gigs of usable disk space on an NTFS drive - System Volume Information! Leftover from vista days.

    Thanks a lot!

    Bob

Page 1 of 2 12 LastLast

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
  •