我構建了一個python桌面webapp與一個相當簡單的燒瓶後端,目前我只通過Chrome服務。我試圖用pywebview打包它,但我遇到了一個困難的路障。Pywebview塊燒瓶應用程序,除非我打開兩個網頁瀏覽
我的代碼看起來像這樣在我的包的主文件:
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()
這幾乎失敗系統,窗口顯示空白和後端沒有任何東西迴應(包括從請求單獨的桌面瀏覽器)。
我無意中發現,如果我啓動兩個線程的WebView,它始終工作:
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()
可能是什麼造成的?我對Python中線程的理解是相當有限的,所以我不確定在哪裏尋找。這可能是pywebview中的錯誤,或者我的線程做錯了什麼?
我也開放給webview部分的替代方案的建議,但我想保留python/flask部分,因爲應用程序已經正常工作。