| | |
how to kill a thread in python?
![]() |
•
•
Join Date: May 2005
Posts: 215
Reputation:
Solved Threads: 16
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.
the run_timer method is started as a new thread using this line of code
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?
Python Syntax (Toggle Plain Text)
#!/usr/bin/env python import os import time import commands import thread import plugin from gui.PopupBox import PopupBox from gui.ConfirmBox import ConfirmBox class PluginInterface(plugin.ItemPlugin): """ this plugin will power down your system using the command "shutdown -h now" after your avi file has finished. you can enable it by adding this to your local conf file plugin.activate('video.autoshutdown') """ def __init__(self): plugin.ItemPlugin.__init__(self) def actions(self, item): self.item = item if item.type == 'video': return [ (self.confirm_start_timer, 'engage autoshutoff') ] else: return [] def confirm_start_timer(self, arg=None, menuw=None): ConfirmBox(text=_('engage autoshutoff for the following avi file\n\ "%s"') % self.item.name, handler=self.start_timer, default_choice=1).show() def start_timer(self, arg=None, menuw=None): box = PopupBox(text=_('you must start your avi file within ' \ 'one minute to prevent the shutdown' \ ' to begin' )) box.show() time.sleep(6) box.destroy() thread.start_new_thread(self.run_timer,()) def run_timer(self): while True: time.sleep(60) if commands.getoutput('ps -ewf').__contains__(self.item.filename)== False: os.system('shutdown -h now') break
Python Syntax (Toggle Plain Text)
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.
Hi shanenin,
Here's one way of doing it: 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.
Here's one way of doing it:
Python Syntax (Toggle Plain Text)
import sys, os, time, thread # Keep track of thread PIDs t1, t2 = 0, 0 # The major thread function def f(timeslice): # Set t2 to the PID for this thread global t2 t2 = os.getpid() # Loop, do something after every timeslice while True: # You would do something intelligent here time.sleep(timeslice) # Set t1 to the PID for the parent thread t1 = os.getpid() # Start a new thread thread.start_new_thread(f, (10,)) # Wait a bit and print the two PIDs time.sleep(1) print "PIDs:", t1, t2 # Give the user an option to stop the thread and the program while True: answer = raw_input("Stop thread now? Y/N: ") if answer in [ "Y", "y" ]: os.popen("kill -9 "+str(t2)) break # Do something here - maybe cleanup? # Terminate gracefully sys.exit(0)
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
![]() |
Similar Threads
- How do I kill an old thread? (IT Professionals' Lounge)
- Python Stat points... (Python)
- Trying to kill a running thread - Python (Python)
Other Threads in the Python Forum
- Previous Thread: Creating Forms with Boa
- Next Thread: wxpython Setfocus problem
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input itunes java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite ssh statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib





