#!/usr/bin/python import pygtk import gtk import gtk.glade import gobject import sys import threading import time import Queue import random class AppGui: def __init__(self, qIn, qOut): """ In this init we are going to display the main serverinfo window """ self.qIn = qIn self.qOut = qOut self.gladefile=sys.argv[1] self.wTree = gtk.glade.XML(self.gladefile) self.mainWindow = self.wTree.get_widget("window1") self.mainWindow.connect("delete_event", self.destroy) self.goButton = self.wTree.get_widget("goButton") self.goButton.connect("toggled", self.runTest) self.goButtonState = False # used to track if we start or cancel a test self.progressBar = self.wTree.get_widget("progressbar1") self.timeoutHandler=gobject.timeout_add(100,self.processGuiUpdates) #looks for data from samplers self.iterations = 0.0 # place holder variable - should be removed when real thread code gets here self.progress = 0.0 # progress bar state def delete_event(self, widget, event, data=None): # If you return FALSE in the "delete_event" signal handler, # GTK will emit the "destroy" signal. Returning TRUE means # you don't want the window to be destroyed. # This is useful for popping up 'are you sure you want to quit?' # type dialogs. #print "delete event occurred" # Change FALSE to TRUE and the main window will not be destroyed # with a "delete_event". return False def destroy(self, widget, data=None): #print "destroy signal occurred" gtk.main_quit() def progressBarUpdates(self): self.progress += 0.01 if self.progress < 1: self.progressBar.set_fraction(self.progress) return True else: self.progressBar.set_fraction(1.0) self.goButton.set_active(False) # generates toggled signal and calls runTest() print("bar is done.") return False def runTest(self, widget, data=None): if self.goButtonState == True: # we have a test running # send a kill signal to a thread self.goButtonState = False self.goButton.set_label("Go!") else: self.goButtonState = True self.goButton.set_label("Cancel") self.progressBar.set_fraction(0.0) hours = self.wTree.get_widget("spinbuttonHours").get_value() minutes = self.wTree.get_widget("spinbuttonMinutes").get_value() seconds = self.wTree.get_widget("spinbuttonSeconds").get_value() duration = (hours * 3600) + (minutes * 60) + seconds if duration <= 0: # pop up dialog if test is less than 10 seconds long md = gtk.MessageDialog(self.mainWindow, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_CLOSE, "Sample time is less than 10 seconds") md.run() md.destroy() self.goButton.set_active(False) #makes a recursvie call to this functions return self.progressBarHandler = gobject.timeout_add(int(duration * 10), self.progressBarUpdates) self.qIn[0].put(self.iterations) # start thread operations (door and flow control) def processGuiUpdates(self): """Handle all jobs currently in qOut, if any""" while self.qOut[0].qsize(): try: job=self.qOut[0].get(0) #print "We have to deal with a job" print "Job value is ", repr(job) self.iterations = job except Queue.Empty: #print "qOut is empty" pass return True class ThreadManager: def __init__(self): self.running=True gtk.gdk.threads_init() self.qIn=[Queue.Queue(), Queue.Queue(), Queue.Queue()] self.qOut=[Queue.Queue(), Queue.Queue(), Queue.Queue()] self.gui=AppGui(self.qIn,self.qOut) #create gui #self.progressThread=threading.Thread(target=self.progressThread) self.aliCatThread=threading.Thread(target=self.aliCatThread) #i/o thread self.aliCatThread.setDaemon(True) self.aliCatThread.start() gtk.main() self.running=False def aliCatThread(self): """ This is where the blocking I/O job is being done. """ while self.running: while self.qIn[0].qsize(): #print "There is stuff in qIn" try: job=self.qIn[0].get(0) #self.gui.currentJobId=job.id print "Let's process a job" time.sleep(random.random()*6) #job.result='we would store the resutl here' #self.gui.currentJobId=None self.qOut[0].put(job + 0.1) except Queue.Empty: pass time.sleep(2) if __name__ == "__main__": foo = ThreadManager() # creates a sensor monitoring thread and a gui thread