Python 在桌面应用程序中正确集成wsgiref.simple_服务器和Flask
我在桌面应用程序中集成由Python 在桌面应用程序中正确集成wsgiref.simple_服务器和Flask,python,flask,wsgi,Python,Flask,Wsgi,我在桌面应用程序中集成由wsgiref.simple_服务器提供服务的Flask应用程序时遇到问题 我们的问题是,当用户进入通过CEF浏览器显示应用程序的特殊软件区域时,我们必须启动和停止服务器 除了我们必须停止和启动应用程序时,所有这些都可以正常工作 这样的代码会发生一件奇怪的事情: import os import time import threading from wsgiref.simple_server import make_server from flask import Fla
wsgiref.simple_服务器提供服务的Flask
应用程序时遇到问题
我们的问题是,当用户进入通过CEF浏览器显示应用程序的特殊软件区域时,我们必须启动和停止服务器
除了我们必须停止和启动应用程序时,所有这些都可以正常工作
这样的代码会发生一件奇怪的事情:
import os
import time
import threading
from wsgiref.simple_server import make_server
from flask import Flask
def start(host='127.0.0.1', port=5000, app=None):
global SERVER, THREAD
SERVER = make_server(host, port, app)
SERVER.timeout = 1
threading.Thread(target=SERVER.serve_forever, daemon=True).start()
def stop():
global SERVER
SERVER.shutdown()
def get_app():
APP = Flask(__name__)
@APP.route("/")
def hello():
return "Hello World!"
return APP
if __name__ == '__main__':
print("First start", flush=True)
start("0.0.0.0", 5000, get_app())
time.sleep(10)
print("Start stop procedure", flush=True)
stop()
print("Stop", flush=True)
print("Second start", flush=True)
start("0.0.0.0", 5000, get_app())
time.sleep(10)
print("Start stop procedure", flush=True)
stop()
print("Stop", flush=True)
非常奇怪的是,在服务器理解必须关闭之前,我需要一个请求
$ python test.py
First start
127.0.0.1 - - [29/Oct/2018 17:04:36] "GET / HTTP/1.1" 200 12
Start stop procedure
127.0.0.1 - - [29/Oct/2018 17:04:46] "GET / HTTP/1.1" 200 12 # the unwanted request
Stop
Second start
127.0.0.1 - - [29/Oct/2018 17:04:56] "GET / HTTP/1.1" 200 12
Start stop procedure
127.0.0.1 - - [29/Oct/2018 17:05:01] "GET / HTTP/1.1" 200 12# the unwanted request
Stop
我控制不了那件事。定制的wsgi应用程序不会出现这种情况
有没有合适的方法来停止和启动烧瓶应用程序?网络上的所有解决方案似乎都不起作用我怀疑我们需要使用参数来实现永远服务功能
这个解决方案似乎对我有效
import time
import threading
from wsgiref.simple_server import make_server
from flask import Flask
def get_app():
APP = Flask(__name__)
@APP.route("/")
def hello():
return "Hello World!"
return APP
class WsgiRefServer(threading.Thread):
def __init__(self, wsgi_app, host='127.0.0.1', port=5000):
super().__init__()
self._server = make_server(host, port, wsgi_app)
def run(self):
self._server.serve_forever(poll_interval=0.5)
def stop(self):
self._server.shutdown()
self.join()
if __name__ == '__main__':
http_thread = WsgiRefServer(get_app())
http_thread.start()
time.sleep(10)
http_thread.stop()