thoughts on long running scripts?
#1
PHP Code:
while True:
    if 
some xbmc variable == True:
        
# do something
    
else:
        
# oh its not true, do nothing
        
pass
    xbmc
.sleep(1000

Is this an ok idea in XBMC? are there better ways to accomplish this?
Reply
#2
That's bad. Incorporate "not xbmc.abortRequested" in the while loop
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#3
(2013-08-12, 22:29)Martijn Wrote: That's bad. Incorporate "not xbmc.abortRequested" in the while loop

Ok. So that's just signal handling. Thanks for the tip!

Beyond that, is the concept fine? an infinite loop in a service plugin?
Reply
#4
yes. as long as it dies when it is told to die.
Reply
#5
You can also go the async-callback road.

Have a look to the onAbortRequested()-method in xbmc.Monitor class.

Btw, from the Python point of view some kind of XBMCAbortRequested-Exception raising would combine both methods in a more "pythonic"-way...
My GitHub. My Add-ons:
Image
Reply
#6
(2013-08-13, 10:19)sphere Wrote: You can also go the async-callback road.

Have a look to the onAbortRequested()-method in xbmc.Monitor class.

Btw, from the Python point of view some kind of XBMCAbortRequested-Exception raising would combine both methods in a more "pythonic"-way...

So my code should look more like this:

def monitor = xmbc.Monitor()

RUN = True
while RUN:
if monitor.onAbortRequested():
RUN = False

Is there a plugin any of you guys are aware of that utilizes such an approach?

thanks
Reply
#7
see service.* in repo. all of em should do it more or less.
Reply
#8
(2013-08-13, 17:03)v0idnull Wrote: So my code should look more like this:

def monitor = xmbc.Monitor()

RUN = True
while RUN:
if monitor.onAbortRequested():
RUN = False
No. If you want to poll for the abort requested just do it the way Martijn and Spiff said.
If you know what "async callback" means, you should already know what that means and how to use it Wink

(2013-08-13, 17:03)v0idnull Wrote: Is there a plugin any of you guys are aware of that utilizes such an approach?
See my Nyan Cat Screensaver for an example. But it does not wait for the onAbortRequested-callback but for the onScreensaverDeactivated() callback.
My GitHub. My Add-ons:
Image
Reply
#9
which reminds me. the cat needs a dim!
Reply
#10
(2013-08-13, 17:08)sphere Wrote:
(2013-08-13, 17:03)v0idnull Wrote: So my code should look more like this:

def monitor = xmbc.Monitor()

RUN = True
while RUN:
if monitor.onAbortRequested():
RUN = False
No. If you want to poll for the abort requested just do it the way Martijn and Spiff said.
If you know what "async callback" means, you should already know what that means and how to use it Wink

(2013-08-13, 17:03)v0idnull Wrote: Is there a plugin any of you guys are aware of that utilizes such an approach?
See my Nyan Cat Screensaver for an example. But it does not wait for the onAbortRequested-callback but for the onScreensaverDeactivated() callback.

AHH I think I am beginning to better understand now.

so if I write a class that extends xbmc.Monitor, instantiate it, then its callbacks will automatically be called by XBMC?

Based on your screensaver code, something like this may work:

Code:
class MyMonitor(xbmc.Monitor):
    def __init__(self, service):
        self.service = service

    def onAbortRequested(self):
        self.service.stop()


class MyService(object):
    def __init__(self):
        self.monitor = MyMonitor(service=self)
        self.run = True
    
    def loop(self):
        while self.run:
            # do my loop
            xbmc.sleep(2000)

    def stop(self):
        self.run = False
        
service = MyService()
service.loop()

If this is case, then this is nice and clean. Keeps logic for controlling the loop out of it loop itself.

so if I'm correct, does that mean I can restart the loop? eg:

Code:
class MyMonitor(xbmc.Monitor):
    def __init__(self, service):
        self.service = service

    def onAbortRequested():
        self.service.stop()

    def onScreensaverActivated():
        self.service.stop()

    def onScreensaverDeactivated():
        self.service.loop()


class MyService(object):
    def __init__(self):
        self.monitor = MyMonitor(service=self)
        self.run = True
    
    def loop(self):
        while self.run:
            # do my loop
            xbmc.sleep(2000)

    def stop(self):
        self.run = False
        
service = MyService()
service.loop()

Will this do what I think it will do? I can't seem to find clear documentation on this so thanks everyone for your patience and help.
Reply

Logout Mark Read Team Forum Stats Members Help
thoughts on long running scripts?0