I need some help regarding how to kill or terminate the thread for xbmc media application as I'm using python version 2.6. Iam able to open the thread when I hit the enter button on the keyboard, but I'm not able to kill or terminate the thread when I hit the backspace button.
Here is the code:
import urllib2
import StringIO
import threading
ACTION_ENTER = 7
ACTION_BACKSPACE = 110
def allchannels_timer(self):
for i in range(1):
time.sleep(0.3)
self.getControl(4202).setLabel("0%")
#DOWNLOAD THE XML SOURCE HERE
url = ADDON.getSetting('allchannel.url')
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
response.close()
if action == ACTION_BACKSPACE:
if img1_yellow == False:
self.getControl(4202).setLabel("")
#cancel the connection and close the database
if action == ACTION_ENTER:
if img1_yellow == True:
self.thread = threading.Thread(target=self.allchannels_timer)
self.thread.setDaemon(True)
self.thread.start()
Do you know how I can kill or terminate the thread when I hit the backspace button?
What method do I need to use to kill or terminate the thread?
There is no method that can kill/terminate a thread. Normally what you need to do is some how indicate to the thread that it should shut down, and have code in the thread itself that checks for that indication, and gracefully shuts down the thread when its received.
This can't easily be applied to your code, though, since the vast majority of your threaded function's run time is spent in a single blocking I/O call. What you could do, is make that call in a separate process, which can be terminated, rather than a separate thread. That way, your thread can wait in a loop for the process to complete, but also wait for a signal from the main thread that it should terminate.
Normally I would suggested moving the whole threaded function to a separate process, but it looks like you're trying to update a GUI element from the thread, which probably will not work properly in a separate process. (Usually it doesn't work properly from a separate thread, either, though.)
import urllib2
import time
import threading
from multiprocessing import Process, Queue
ACTION_ENTER = 7
ACTION_BACKSPACE = 110
event = threading.Event()
def do_request(url, q):
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
response.close()
q.put(data)
def allchannels_timer(self):
for i in range(1):
time.sleep(0.3)
self.getControl(4202).setLabel("0%")
#DOWNLOAD THE XML SOURCE HERE
url = ADDON.getSetting('allchannel.url')
q = Queue()
p = Process(target=do_request, args=(url, q))
p.start()
while p.is_alive():
if event.is_set():
p.terminate()
return
time.sleep(.3)
# Make sure you don't read from the queue after calling terminate()
data = q.get()
if action == ACTION_BACKSPACE:
if img1_yellow == False:
self.getControl(4202).setLabel("")
event.set()
self.thread.join()
if action == ACTION_ENTER:
if img1_yellow == True:
event.clear()
self.thread = threading.Thread(target=self.allchannels_timer)
self.thread.setDaemon(True)
self.thread.start()