0

I am prototyping an android app with bottle and sl4a. Running into some trouble with some code. I am using multiprocessing to set a 5 second delay on a function that opens up a webview to the localhost so when i run the server it has time to setup before the webview requests the site. For some reason it is not opening up a webview. ( script is not exiting and leaves no errors to figure out what is working incorrectly) I can run the server in one script and run an alternate script that opens up the webview and it works just fine. Can anyone see a problem in my code as I do not want to have separate scripts since I plan on wrapping this all up in an apk with sl4a and pythonforandroid.

I stripped most of my code for simplicity.

from bottle import route, run, template, static_file, redirect, request
from multiprocessing import Process 
import android


def showurl():
    time.sleep(5)
    droid.webViewShow("http:\/\/localhost:8080")


if __name__ == '__main__':
    droid = android.Android()
    port = int(os.environ.get('PORT', 8080))
    p = Process(target=showurl)
    p.start
    run(host='localhost', port=port, debug=True)

Thanks in advance!

michael g
  • 603
  • 7
  • 14

1 Answers1

0

Figured it out. Made the code a little more verbose for explanation.

from bottle import route, run, template, static_file, redirect, request
from multiprocessing import Process

def startserver():
    port = int(os.environ.get('PORT', 8080))
    run(host='localhost', port=port, debug=True)

def showurl():
    droid.webViewShow("http:\/\/localhost:8080")  

@route('/')
def index():
    return static_file('index.html', root='')


if __name__ == '__main__':
    droid = android.Android()
    server = Process(target=startserver)
    webview = Process(target=showurl)
    server.start()
    time.sleep(1)
    webview.start()

I had no problem getting this to work once I put both the server and webview through the Process() method from multiprocessing with a (1) second delay starting the webview to allow time for the server to setup.

A fully functioning native/html app all in python. FTW!!!

michael g
  • 603
  • 7
  • 14