how to kill a thread in python?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

how to kill a thread in python?

 
0
  #1
Dec 13th, 2005
I have a very simple plugin I wrote for freevo. below is the plugin. it will shutdown your computer after your avi file has finished playing, kind of like a sleep timer.
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import time
  5. import commands
  6. import thread
  7.  
  8. import plugin
  9.  
  10. from gui.PopupBox import PopupBox
  11. from gui.ConfirmBox import ConfirmBox
  12.  
  13. class PluginInterface(plugin.ItemPlugin):
  14. """
  15. this plugin will power down your system using the command
  16. "shutdown -h now" after your avi file has finished.
  17. you can enable it by adding this to your local conf file
  18. plugin.activate('video.autoshutdown')
  19. """
  20. def __init__(self):
  21. plugin.ItemPlugin.__init__(self)
  22.  
  23. def actions(self, item):
  24. self.item = item
  25. if item.type == 'video':
  26. return [ (self.confirm_start_timer, 'engage autoshutoff') ]
  27. else:
  28. return []
  29.  
  30. def confirm_start_timer(self, arg=None, menuw=None):
  31. ConfirmBox(text=_('engage autoshutoff for the following avi file\n\
  32. "%s"') % self.item.name,
  33. handler=self.start_timer, default_choice=1).show()
  34.  
  35. def start_timer(self, arg=None, menuw=None):
  36. box = PopupBox(text=_('you must start your avi file within ' \
  37. 'one minute to prevent the shutdown' \
  38. ' to begin' ))
  39. box.show()
  40. time.sleep(6)
  41. box.destroy()
  42. thread.start_new_thread(self.run_timer,())
  43.  
  44. def run_timer(self):
  45. while True:
  46. time.sleep(60)
  47. if commands.getoutput('ps -ewf').__contains__(self.item.filename)== False:
  48. os.system('shutdown -h now')
  49. break
the run_timer method is started as a new thread using this line of code
  1. thread.start_new_thread(self.run_timer,())

the run timer_method checks at an interval of every minute to see if an avi file is playing, if the avi is no longer playing, it will send the computer the shutdown command. I would like to be able to abort the shutdown if possible. Is their a way to kill the thread that I started, maybe some sort of kill method?
In a perfect world exceptions would not be needed.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 29
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: how to kill a thread in python?

 
0
  #2
Dec 14th, 2005
Hi shanenin,

Here's one way of doing it:
  1. import sys, os, time, thread
  2. # Keep track of thread PIDs
  3. t1, t2 = 0, 0
  4. # The major thread function
  5. def f(timeslice):
  6. # Set t2 to the PID for this thread
  7. global t2
  8. t2 = os.getpid()
  9. # Loop, do something after every timeslice
  10. while True:
  11. # You would do something intelligent here
  12. time.sleep(timeslice)
  13. # Set t1 to the PID for the parent thread
  14. t1 = os.getpid()
  15. # Start a new thread
  16. thread.start_new_thread(f, (10,))
  17. # Wait a bit and print the two PIDs
  18. time.sleep(1)
  19. print "PIDs:", t1, t2
  20. # Give the user an option to stop the thread and the program
  21. while True:
  22. answer = raw_input("Stop thread now? Y/N: ")
  23. if answer in [ "Y", "y" ]:
  24. os.popen("kill -9 "+str(t2))
  25. break
  26. # Do something here - maybe cleanup?
  27. # Terminate gracefully
  28. sys.exit(0)
You can keep doing stuff in the parent thread after you have killed the spawned thread. Alternatively, you could spawn two threads, one which queries the user for input and the other which plays the AVI/shuts down. The first could write to some global flag, which the second would check every timeslice - if the flag goes up, the second thread self-terminates.

An alternative to calling "kill" explicitly is to subclass the Thread class and call the join() method on your thread instance. See here for details.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: how to kill a thread in python?

 
0
  #3
Dec 15th, 2005
thanks. I appreciate the nice example :-)
In a perfect world exceptions would not be needed.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum


Views: 31559 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC