Stopping a thread from an imported file

Marc mnations at airmail.net
Fri Mar 29 12:18:06 EST 2002


Hi,

I am writing an application that has a front end Tkinter GUI and a
back end process. The back end process is being imported into the GUI,
which acts as the main thread in the program. When the 'GO' button is
clicked, the GUI starts a thread which calls a function that sends
control to the back end.

The problem is that when this function has ended, the thread does not
die. So when the button is clicked to start the application again, it
won't start because it thinks the thread is still active.

I have another thread within the GUI application that simply keeps
track of elapsed time. I don't have the same problem with this thread
which actually restarts every time the button is clicked. That, to me,
is even more confusing, since I know that thread is still running. And
since my experience with threads is limited, I'm simply going based on
what works and what doesn't to get this program working, and I can't
reason out what I've seen.

Which leads me to two questions:
1) When a function in an imported file completes, how does the thread
in the main file know to stop.
2) If you can't restart a thread while it is active (which is the
error I receive in the first thread) then how can the second thread
simply restart when it is called again.

Here's the relevant code:

class GuiPart:
        def __init__(self, master, endCommand, thread1, thread2):
            self.endCommand = endCommand
            self.thread1 = thread1
            self.thread2 = thread2

            self.master = master
	    self.mainFrame = Frame(master)

            self.mainWindow = Frame(self.mainFrame)

            self.mainFrame.pack()
            self.mainFrame.master.title("Running OCN cards")
            self.mainWindow.pack()      
            self.createWidgets()
            
	def validation(self):
	    self.startTime = time.time()
	    flag = 0
	    
	    flag = self.validateIP( self.ipaddr.get() )
	    validOcn = self.var.get()
	    if validOcn == '':
		    showerror(title='Invalid OCN',message='Please select an OCN
Type')
		    flag = 0
	    else:
		    flag = flag * 1
	    
	    if flag:
		    self.go_thread()    

	def go_thread(self):
		self.thread1.setDaemon(1)
		self.thread2.setDaemon(2)
		self.thread1.start()
		self.thread2.start()

<...snip...>
class ThreadedClient:
    def __init__(self, master):

        self.master = master
        self.ok = 1
        self.thread1 = Thread(target = self.ocnRun)
        self.thread2 = Thread(target = self.elapsedTime)
        self.gui = GuiPart(master, self.endApplication, self.thread1,
self.thread2)

        self.periodicCall()

    def periodicCall(self):
        if not self.ok:
	    #os._exit(0)
	    sys.exit(0)
	self.master.after(100, self.periodicCall)

    def ocnRun(self):
	    print "In ocnRun"
	    counter, total =  0, 0
	    for testgroup, titles, groupRow, groupCol in self.gui.testGroups:
			for module, row, col, status, color in testgroup:
				modFlag = getattr(self.gui.modVar, module).get()
				total = modFlag * pow( 2, counter ) + total
				counter = counter + 1

	    self.gui.slot = self.gui.list.curselection()
	    printAll( self.gui.ipaddr.get(), self.gui.var.get(),
self.gui.list.get(self.gui.slot), total, self.gui.port.get(),
		      self.gui.filename_field.get(), self.gui.password.get(),
self.gui.debug_field.get() , self.gui.debugVar.get(),
		      self.gui, self.gui.iterations.get() )

    def elapsedTime(self):
		curTime = time.time()
		eTime = curTime - self.gui.startTime

		hours = str( int(eTime / 3600) )
		mins = str( int(eTime / 60) )
		secs = str( int(eTime) )

		if atoi(hours) < 10:
			hours = '0' + hours
		if atoi(mins) < 10:
			mins = '0' + mins
		if atoi(secs) < 10:
			secs = '0' + secs
	
		elapsedTime = hours + ':' + mins + ':' + secs
		self.gui.elapsTime.insert(END, elapsedTime)
		self.gui.master.after(1000, self.elapsedTime)

    def endApplication(self):
        self.ok = 0

""" Begin Main Processing """
root=Tk()
client = ThreadedClient(root)
root.mainloop()



The first thread calls ocnRun which calls a function from an imported
file. Somehow I need to tell ocnRun that PrintAll has stopped. But I
assumed that would simply happen when PrintAll ran to completion.

Anyway, I think that's all the relevant information. I've read as much
stuff on threads from this newsgroup and other resources as I could
find, but still can't find the answer. Any help would be appreciated.

Thanks ahead of time,
Marc



More information about the Python-list mailing list