943,752 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 49993
  • Python RSS
Dec 13th, 2005
0

how to kill a thread in python?

Expand Post »
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.
Python Syntax (Toggle Plain Text)
  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
Python Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005
Dec 14th, 2005
0

Re: how to kill a thread in python?

Hi shanenin,

Here's one way of doing it:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 41
Solved Threads: 31
Junior Poster
G-Do is offline Offline
146 posts
since Jun 2005
Dec 15th, 2005
0

Re: how to kill a thread in python?

thanks. I appreciate the nice example :-)
Reputation Points: 10
Solved Threads: 17
Posting Whiz in Training
shanenin is offline Offline
217 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Creating Forms with Boa
Next Thread in Python Forum Timeline: wxpython Setfocus problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC