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: 94
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: 123
Joined: May 2009
Reputation: 0
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: 94
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: 94
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,714
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: 94
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,341
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: 94
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: 94
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: 24,523
Joined: Oct 2003
Reputation: 138
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
Post Reply