Code Monkey home page Code Monkey logo

Comments (1)

RobertFlatt avatar RobertFlatt commented on September 3, 2024

@mperezfra
I changed the service to resume on app on_resume()
Because the stop() code does not work, I previously made these changes #9 (comment)

In on_resume(), I detect no response from the service, and restart it. Restart requires an elegant termination.

I expect there are issues that I have not considered, but this appears to work on Windows and Android.

Code fragments:

main .py:
server.bind(b'/alive', self.alive_response)

# ask if the service is alive
def ask_service_alive(self):
    self.im_alive = False
    self.client.send_message(b'/alive', [])
    # check for response after a short time
    Clock.schedule_once(self.check_alive,0.1)

# the response, which we may not get
def alive_response(self, message):
    self.im_alive = message.decode('utf8') == 'alive'

# check for no response
def check_alive(self, dt):
    if not self.im_alive:
        # lets hope whatever killed the service cleaned up afterwards
        self.start_service()

# a user might hope this would resume a service
def on_resume(self):
    if self.service:
        self.ask_service_alive(True)

service.py :

def alive():
    CLIENT.send_message(
        b'/alive',
        ['alive'.encode('utf8'), ],
    )

stopped = False

def stop(*_):
    global stopped
    stopped = True
    
if __name__ == '__main__':
    SERVER = OSCThreadServer()
    SERVER.listen('localhost', port=3000, default=True)
    SERVER.bind(b'/ping', ping)
    SERVER.bind(b'/stop', stop)
    SERVER.bind(b'/alive', alive)
    SERVER.getaddress()
    while True:
        sleep(1)
        if stopped:
            break
        send_date()
    SERVER.terminate_server()
    sleep(0.1)
    SERVER.close()

The choice to restart the service on_resume() is totally arbitrary. One could perhaps have two services restarting each other......

The unanswered question is what causes Android to stop the service? ( other than because it can)
These are not exactly helpful:
https://developer.android.com/guide/components/services#Basics
https://developer.android.com/reference/android/app/Service

More interesting how can one write a service that is unlikely to be stopped?
I have not seen an Android stop yet, but I'm restarting; and because of the way the demo is written I wouldn't.

from kivy_service_osc.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.