Stopping a thread from an imported file

Marc mnations at airmail.net
Sat Mar 30 13:41:55 EST 2002


One more thing that I've found. When I take the external file and
simply import it into the main file, I have the same problem. So
apparently the problem is not with the external file but with the way
the application is viewing the threads. I have debug statements
throughout telling me when the thread isActive or not active. Right
before I make the second call it shows me that the thread is not
active. Yet when I try to restart the thread, the error comes back
that it's already started. Here is the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python22\lib\lib-tk\Tkinter.py", line 1292, in __call__
    return apply(self.func, args)
  File "threadocn.py", line 46, in validation
    self.go_thread()
  File "threadocn.py", line 49, in go_thread
    self.thread1.start()
  File "C:\Python22\lib\threading.py", line 384, in start
    assert not self.__started, "thread already started"
AssertionError: thread already started

Thanks,
Marc



mnations at airmail.net (Marc) wrote in message news:<4378fa6f.0203290918.21be5409 at posting.google.com>...
> 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