[Python] Pywebview blocks flask app unless I open two webviews

Discussão em 'Python' iniciado por Stack, Janeiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I built a python desktop webapp with a fairly simple flask backend which I'm currently serving simply through Chrome. I'm trying to package it using pywebview, but I ran into an intestesting roadblock.

    I have code that looks like this in the main file of my package:

    import os
    import sys
    from flasher import app

    import httplib

    import webview
    import threading


    def start_prod_server():
    runserver(debug=False, reloader=False)

    def start_gui():
    webview.create_window("MyAppName", "http://localhost:5000", height=1000)

    def runserver(debug=True, reloader=False):
    port = int(os.environ.get('PORT', 5000))
    url = "http://localhost:{}".format(port)

    print("ready!")
    app.run(host='::', port=port, debug=debug, use_reloader=reloader)


    if __name__ == '__main__':
    t = threading.Thread(target=start_prod_server)
    t.daemon = True
    t.start()

    # This never works
    threading.Timer(1.5, start_gui).start()

    sys.exit()


    This fails almost systematically, the window shows up blank and the backend doesn't respond to anything (including requests from a separate desktop browser).

    I discovered accidentally that if I start two webview threads, it always works:

    if __name__ == '__main__':
    t = threading.Thread(target=start_prod_server)
    t.daemon = True
    t.start()

    # This works every time
    threading.Timer(1.5, start_gui).start()
    threading.Timer(1.5, start_gui).start()

    sys.exit()


    What could be causing this? My understanding of threading in Python is fairly limited, so I'm not sure where to look. Is this likely to be a bug in pywebview, or am I doing something wrong with my threads?

    I'm also open to suggestions of alternatives for the webview part, but I want to keep the python/flask part since the app already works fine as it is.

    Continue reading...

Compartilhe esta Página