shanenin Posted December 13, 2005 Report Share Posted December 13, 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.#!/usr/bin/env pythonimport osimport timeimport commandsimport threadimport pluginfrom gui.PopupBox import PopupBoxfrom gui.ConfirmBox import ConfirmBoxclass 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') breakthe run_timer method is started as a new thread using this line of codethread.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? Quote Link to post Share on other sites
jcl Posted December 13, 2005 Report Share Posted December 13, 2005 (edited) Thread cancellation is not for the faint of heart. The first thing do to is make sure you absolutely need the extra thread. Timers aren't exactly a rare commodity in multimedia software; Freevo should (haha) provide a timer API that you can use. Or even better, an event interface that let you register your shutdown function as a callback.The second thing you should do is forget about cancelling the thread by force and instead look at notify it when it's no longer needed. For example, add a (lock-protected) flag that it checks at every update. When you want to kill the thread, toggle the flag. (This is potentially useful even if you don't use an explicit thread.)The third thing you should do is check whether the plugin API lets you check the status of the movie. The call to 'ps' is a bit silly considering that the plugin is part of the program you're inspecting. If the plugin API provides neither timers nor access to Freevo's state, consider fixing the damn API Edited December 13, 2005 by jcl Quote Link to post Share on other sites
shanenin Posted December 14, 2005 Author Report Share Posted December 14, 2005 The third thing you should do is check whether the plugin API lets you check the status of the movie. The call to 'ps' is a bit silly considering that the plugin is part of the program you're inspecting.the call to 'ps' was my inspiration to write the plugin :-) I also did think it was kind of "silly" way to do it. As to using the freevo API I need to find some docs. I probably might find something on their wiki. As of now this is the only doc I have used, or seenhttp://freevo.sourceforge.net/cgi-bin/doc/DevelopPluginsThanks for all of your suggestions :-) Quote Link to post Share on other sites
jcl Posted December 14, 2005 Report Share Posted December 14, 2005 the call to 'ps' was my inspiration to write the plugin :-) I also did think it was kind of "silly" way to do it.Ah, well in that case it's fine As to using the freevo API I need to find some docs. I probably might find something on their wiki. As of now this is the only doc I have used, or seenhttp://freevo.sourceforge.net/cgi-bin/doc/DevelopPluginsYeah, I noticed the documentation was a bit skimpy when I went looking for timers. It looks like they expect you to RTFS and figure it out. Nice of them. Quote Link to post Share on other sites
shanenin Posted December 14, 2005 Author Report Share Posted December 14, 2005 RTFS'S' must refer to scripts? Quote Link to post Share on other sites
jcl Posted December 14, 2005 Report Share Posted December 14, 2005 'S' must refer to scripts?'Source'. Quote Link to post Share on other sites
shanenin Posted December 14, 2005 Author Report Share Posted December 14, 2005 I found this simple solutionshutdown -c Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.