سؤال

I am using thread in my python card application.

whenever i press the refresh button i am calling a function using thread, when the function was called, there will be a another function inside the main function.

what i want is whenever the child function ends. I want the thread to be killed or stopped without closing the application or ctrl+ c.

i started the thread like this

def on_refresh_mouseClick(self,event):
thread.start_new_thread( self.readalways,() ) 

in the "readalways" function i am using while loop, in that while loop whenever the condition satisfies it will call continuousread() function. check it:

def readalways(self):
     while 1:
           cardid = self.parent.uhf.readTagId()
           print "the tag id is",cardid
           self.status = self.parent.db.checktagid(cardid)
           if len(self.status) != 0:
               break
     print "the value is",self.status[0]['id']
     self.a = self.status[0]['id']
     self.continuesread() 

def continuesread(self):
 .......
    .......

after this continuesread read function the values that in the thread should be cleared.

Because, if i again click the refresh button a new thread is starting but the some of the values are coming from the old thread.

so i want to kill the old thread when it completes the continuesread function

هل كانت مفيدة؟

المحلول

Please note that different threads from the same process share their memory, e.g. when you access self.status, you (probably) manipulate an object shared within the whole process. Thus, even if your threads are killed when finishing continuesread (what they probably are), the manipulated object's state will still remain the same.

You could either

  • hold the status in a local variable instead of an attribute of self,
  • initialize those attributes when entering readalways,
  • or save this state in local storage of an thread object, which is not shared (see documentation).

The first one seems to be the best as far as I can see.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top