Newbie question: how do I properly end a script?

  Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Post Reply
brulsmurf Offline
Member
Posts: 93
Joined: Oct 2008
Reputation: 0
Post: #1
I have recently started playing around with XBMC scripting. Progress is slow, as I have no experience with Python. But it's fun, and the results are not so bad. One thing I have not yet been able to find out though. Here's the thing: when my script starts, a loop is started that shows some info every minute. That loop should end when the user quits the script. Instead, it gives me an error.

Is there any method called by XBMC to end the script? If so, I could do the cleaning up in that method. Is there a script template available anywhere to look at?
find quote
VictorV Offline
Senior Member
Posts: 124
Joined: May 2009
Reputation: 1
Location: Norway
Post: #2
brulsmurf Wrote:I have recently started playing around with XBMC scripting. Progress is slow, as I have no experience with Python. But it's fun, and the results are not so bad. One thing I have not yet been able to find out though. Here's the thing: when my script starts, a loop is started that shows some info every minute. That loop should end when the user quits the script. Instead, it gives me an error.

Is there any method called by XBMC to end the script? If so, I could do the cleaning up in that method. Is there a script template available anywhere to look at?


You need to test for a condition in the loop.

Code:
while not condition:
    # loop..

# end script
find quote
brulsmurf Offline
Member
Posts: 93
Joined: Oct 2008
Reputation: 0
Post: #3
VictorV Wrote:You need to test for a condition in the loop.

Obviously, but the thing is that I do not know how XBMC tries to end my script. If it calls a method, then I can override/implement that method and make it end my loop. But I need to know the name of the method (or the exact mechanism of how XBMC invokes my script when it tries to stop it) before I can tell the loop to end.

Thanks for helping!
find quote
brulsmurf Offline
Member
Posts: 93
Joined: Oct 2008
Reputation: 0
Post: #4
So maybe this "Newbie question" was not as straightforward as I thought ... Meanwhile, I tried the following:

- override/implement the __del__ method in default.py: seems not to be called at all
- check if the script is called more than once,but with different parameters: seems not to be the case either.

Any ideas, anyone? I really need to do some cleaning up when the script is ended (logout from a website). Besides, the error it returns right now is not the best solution from a user's point of view.

Any help welcome!
find quote
blittan Offline
Team-XBMC Handyman
Posts: 1,739
Joined: Jun 2004
Reputation: 11
Location: Sweden
Post: #5
not sure how you mean really, but to end a script, use sys.exit()

the user must press some button when ending the script, that you have set up in an action.. there you can do you cleanup before exit.

also to not bother the user with errors, encapsulate them in try, except blocks.

try:
myfunkystuff
except:
print "we've encountered an error"

that way if the myfunkystuff fails we just print an error message to the xbmc.log

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
If you don't have the time to read them, please don't take the time to post in this forum!
For troubleshooting and bug reporting please make sure you read this first.
find quote
brulsmurf Offline
Member
Posts: 93
Joined: Oct 2008
Reputation: 0
Post: #6
Hey, thanks for the feedback! I realise that I did not properly explain my situation. Here's a better try:

* When the user starts my script, the script starts a loop that shows some info every minute (using the builtin notification system).
* The script has no GUI!
* So the user stops it by re-selecting it from the Scripts menu
* I can see my script trying to stop when I do that, because it says "MyScript (Stopping)"
* This takes a number of seconds, and then a popup says "Error in MyScript" or something similar (can't test it from here, sorry).

So, to rephrase my question taking Blittan's response into account:
* Does the "try ... except" approach allow me to cleanup when the script ends? Or is it thrown at a higher level because XBMC cannot end my script properly?
* If the "try ... except" does not work for this case, would it be possible to setup an action that respons to the user selecting the script from the Scripts menu to stop it?

I appreciate the help!
find quote
rwparris2 Offline
Team-XBMC Python Developer
Posts: 1,333
Joined: Jan 2008
Reputation: 2
Location: US
Post: #7
imo this is an xbmc bug.

PHP Code:
import time
starttime 
time.time()
while 
time.time() - starttime 5:
    
pass 

start it and close it before 5 seconds pass. xbmc returns a script error.

btw, sys.exit() annoying closes xbmc, so it isn't really a good way to end scripts.

Always read the XBMC online-manual, FAQ and search and search the forum before posting.
For troubleshooting and bug reporting please read how to submit a proper bug report.

If you're interested in writing addons for xbmc, read docs and how-to for plugins and scripts ||| http://code.google.com/p/xbmc-addons/
find quote
brulsmurf Offline
Member
Posts: 93
Joined: Oct 2008
Reputation: 0
Post: #8
Great - I've hit an XBMC bug in my first script attempt Smile

My approach is very similar to rwparris2's, except I use the sleep() function:

Code:
while isRunning:
    startTime = time.time()
    doSomeStuff()
    time.sleep(startTime - time.time + 60)

Should I be looking at threading to overcome this problem? I'm not afraid of that, I worked with threads in other programming languages. But if it will not solve my problem, then obviously I would rather not try that approach.
find quote
brulsmurf Offline
Member
Posts: 93
Joined: Oct 2008
Reputation: 0
Post: #9
Quote:* Does the "try ... except" approach allow me to cleanup when the script ends? Or is it thrown at a higher level because XBMC cannot end my script properly?
No it doesn't. Tried it, didn't work.
Quote:Should I be looking at threading to overcome this problem?
No I shouldn't. Tried it, didn't work.

Now what? I'm stuck already!
find quote
jmarshall Offline
Team-XBMC Developer
Posts: 25,674
Joined: Oct 2003
Reputation: 169
Post: #10
@rwparris2: Mind doing up a trac ticket with everything we need to reproduce, plus some idea of what *should* happen.

What does running the same thing in python do for instance? There's a patch on trac (or is it in SVN) to improve script exit errors.

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


[Image: badge.gif]
find quote
blittan Offline
Team-XBMC Handyman
Posts: 1,739
Joined: Jun 2004
Reputation: 11
Location: Sweden
Post: #11
Doesn't python spawn a new process for each script, eg if you relaunch the script it starts as a new process.

brulsmurf: try and do a .pid file for your script once it starts, then in every loop of the script check to see if that file exists, if it is removed, then quit the script.

eg (try to explain):

User starts script, if no myscript.pid, script creates a myscript.pid (or whatever you want to name a blank file). every time the script loops, check if file exists.

User wants to stop script, launch script again. if myscript.pid exists -> remove it (will cause the running script to exit)

Hope you get how I mean, otherwise, let me know and I will write a small example.

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
If you don't have the time to read them, please don't take the time to post in this forum!
For troubleshooting and bug reporting please make sure you read this first.
find quote
ppic Offline
Skilled Python Coder
Posts: 2,711
Joined: Feb 2009
Reputation: 11
Location: France idf
Post: #12
that the better way to do, but the file will stay if script fails.

[Image: widget]
Passion-XBMC Repository Download your SVN skins and addons
TvTunes Addon Download Play your theme while browsing library
TVshow Next Aired display next aired infos from tvrage.com
Logo Downloader Script download Logo/clearart/show thumbs/poster/banner
Bande-Annonce Allociné plugin watch trailers in french and vo
SportLive Script Live score match info in XBMC (not compatible dharma)
find quote
blittan Offline
Team-XBMC Handyman
Posts: 1,739
Joined: Jun 2004
Reputation: 11
Location: Sweden
Post: #13
yes. not sure if it's possible to get a pid number or check python processes running, that would be even better

Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
If you don't have the time to read them, please don't take the time to post in this forum!
For troubleshooting and bug reporting please make sure you read this first.
find quote
LtChambers Offline
Junior Member
Posts: 33
Joined: Dec 2009
Reputation: 1
Post: #14
If you have proper exception handling, you should always be able to clean up when your script raises an exception. Unless you crash python and/or XBMC, something which I've found is not hard to do, especially where threads are involved. That's probably largely python 2.4 though.
find quote
brulsmurf Offline
Member
Posts: 93
Joined: Oct 2008
Reputation: 0
Post: #15
jmarshall Wrote:What does running the same thing in python do for instance?
Without the XBMC stuff? I'll try during the weekend and get back to you. I'm not sure if aborting the script in Python (CTRL-C I suppose) triggers the same events as ending it in XBMC does. Will find out. Thanks for the suggestion!

blittan Wrote:try and do a .pid file for your script once it starts, then in every loop of the script check to see if that file exists, if it is removed, then quit the script.

I will give it a try. It feels like a workaround, not solving the cause of the problem - but if it works then I'm a happy man.

blittan Wrote:User wants to stop script, launch script again. if myscript.pid exists -> remove it (will cause the running script to exit)

So that's what happens? If the user selects the script for the second time, XBMC tries to launch it a second time - not abort it? But I see no sign of that in the logfiles. I print some info when the script is launched, but it is not printed a second time when I stop it. So why would testing for the .pid file work, while printing to the logfile is not reached at the second launch?

Anyway, will give it a shot and let you know.

ppic Wrote:that the better way to do, but the file will stay if script fails.

If the script fails, I should get an exception within my script and respond to it by deleting the file - right?

LtChambers Wrote:If you have proper exception handling, you should always be able to clean up when your script raises an exception.


Sure, but I think the exception is not raised within my script. In any case I can't catch it even if I surround the entire script with a try .. except block.

LtChambers Wrote:Unless you crash python and/or XBMC, something which I've found is not hard to do, especially where threads are involved.

I ran into that yesterday when I tried to solve things with threads Laugh

Thank you all for being supportive!
find quote
Post Reply