Solved Run flask server in background of KODI
#1
I have flask script that i would like to run in background when KODI is running or when i enter certain addon. I know i could make it work on windows as to run it with subprocess or some other way in CL, but i want it to make it also able to run on android, so it would be better to able to run it just from KODI. I see there is Service addons, but i don't know if i would be able to run flask server with just KODI on windows, android...., or if is there some other way to run flask server on KODI...
Reply
#2
There is no such thing as "Flask server". Flask web-framework uses external WSGI servers to run, and for simple cases you can use Python's built-in WSGIref server. But your server needs to be gratefully shut down on Kodi exit. Kodi itself also includes a WSGI server but I'm afraid it's not well documented.

Here's an example from one of my old addons: https://github.com/romanvm/kodi.yatp/blo...ver.py#L47
It runs a Bottle web-app, but the general principle is the same.
Reply
#3
(2018-11-13, 15:57)Roman_V_M Wrote: There is no such thing as "Flask server". Flask web-framework uses external WSGI servers to run, and for simple cases you can use Python's built-in WSGIref server. But your server needs to be gratefully shut down on Kodi exit. Kodi itself also includes a WSGI server but I'm afraid it's not well documented.

Here's an example from one of my old addons: https://github.com/romanvm/kodi.yatp/blo...ver.py#L47
It runs a Bottle web-app, but the general principle is the same.
It's very simple server with just 50 lines of code.
I found some example of wsgiref, here is the code:
python:
from cgi import parse_qs
from wsgiref.simple_server import make_server
def simple_app(environ, start_response):
status = '200 OK'
headers = [('Content-Type', 'text/plain')]
start_response(status, headers)
if environ['REQUEST_METHOD'] == 'POST':
request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
d = parse_qs(request_body) # turns the qs to a dict
return 'From POST: %s' % ''.join('%s: %s' % (k, v) for k, v in d.iteritems())
else: # GET
d = parse_qs(environ['QUERY_STRING']) # turns the qs to a dict
return 'From GET: %s' % ''.join('%s: %s' % (k, v) for k, v in d.iteritems())
httpd = make_server('', 1337, simple_app)
print "Serving on port 1337..."
httpd.serve_forever()

it works but i can't run anything else, it's just spinning circle and doesn't load code after httpd.serve_forever(). How would i run this in background so everything else would work fine? I am guessing with threads, but maybe there is some other way.

EDIT: It looks like a manage to run it on thread and at least for everything works fine
Reply
#4
You can also plug flask into Kodi's webserver (microhttpd) as a webinterface addon: https://github.com/tamland/kodi-flask-demo
Reply
#5
I made it to work on a thread, but now i have the speed problem. If i run python script with flask outside KODI, everything works fine, but when i run it with wsgiref in KODI, then i have a problem, as when i start video processor is on 100% and video is not smooth, python script just download video every 15-20s and then serve it to KODI, is there any way to solve this to run just in KODI?
Reply

Logout Mark Read Team Forum Stats Members Help
Run flask server in background of KODI0