我正在尝试在Bottle中构建一个小应用程序,并且我认为我会尝试使用pywebview作为查看器。当我运行以下文件时,我得到了webview窗口的两个实例。第一个显示页面,第二个显示旋转轮光标。关闭第二个窗口是关闭Web服务器,我相信,但不会杀死该线程。
为什么会出现两个窗口?
import sys
import threading
from bottle import Bottle, ServerAdapter
import webview
class MyWSGIRefServer(ServerAdapter):
server = None
def run(self, handler):
from wsgiref.simple_server import make_server, WSGIRequestHandler
if self.quiet:
class QuietHandler(WSGIRequestHandler):
def log_request(*args, **kw): pass
self.options['handler_class'] = QuietHandler
self.server = make_server(self.host, self.port, handler, **self.options)
self.server.serve_forever()
def stop(self):
# self.server.server_close() <--- alternative but causes bad fd exception
self.server.shutdown()
app = Bottle()
listen_addr = 'localhost'
listen_port = 8080
server = MyWSGIRefServer(host='localhost', port=8080)
@app.route('/')
def hello():
return "Hello World!"
def start_server():
app.run(server=server, reloader=True)
try:
print(threading.enumerate())
serverthread = threading.Thread(target=start_server)
serverthread.daemon = True
print("starting web server")
serverthread.start()
print("starting webview")
webview.create_window('bottle test', "http://localhost:8080/")
print("webview closed. closing server")
sys.exit()
server.stop()
except Exception as ex:
print(ex)
答案 0 :(得分:1)
运行服务器时问题是使用reloader=True
。将其设置为False
会阻止第二个窗口出现。