LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2005, 11:14 AM   #1
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
how to kill a thread in python


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.
Code:
#!/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
the run_timer method is started as a new thread using this line of code
Code:
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?
 
Old 12-13-2005, 04:58 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Note: this is untested.

Wrap it in an exception:
Code:
try:
    thread.start_new_thread(self.run_timer,())
except KeyboardInterupt:
    sys.exit(1)
 
Old 12-13-2005, 11:13 PM   #3
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
thanks for the suggestion :-)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to kill specific python script melinda_sayang Linux - General 2 12-04-2005 03:31 AM
How to kill a user thread in Linux.... Help me rajsun Programming 4 04-20-2005 04:13 AM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
Please move my thread or kill it or something. :-( gplacek Slackware 1 08-27-2004 01:25 PM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration