Revert "Running Flask webserver in another thread instead than another process"

This reverts commit 0a5c42fdfd.
This commit is contained in:
Fabio Manganiello 2019-02-21 16:15:06 +01:00
parent 0a5c42fdfd
commit 6e1b6686c2

View file

@ -152,8 +152,8 @@ class HttpBackend(Backend):
self.redis_queue = redis_queue self.redis_queue = redis_queue
self.dashboard = dashboard self.dashboard = dashboard
self.maps = maps self.maps = maps
self.server_proc = None
self.disable_websocket = disable_websocket self.disable_websocket = disable_websocket
self.webserver_thread = None
self.websocket_thread = None self.websocket_thread = None
self.redis_thread = None self.redis_thread = None
self.redis = None self.redis = None
@ -192,12 +192,9 @@ class HttpBackend(Backend):
if redis: if redis:
redis.rpush(self.redis_queue, str(stop_evt)) redis.rpush(self.redis_queue, str(stop_evt))
if self.app: if self.server_proc:
stop_func = request.environ.get('werkzeug.server.shutdown') self.server_proc.terminate()
if stop_func is None: self.server_proc.join()
self.logger.warning('Werkzeug server not running')
return
stop_func()
def notify_web_clients(self, event): def notify_web_clients(self, event):
""" Notify all the connected web clients (over websocket) of a new event """ """ Notify all the connected web clients (over websocket) of a new event """
@ -758,17 +755,11 @@ class HttpBackend(Backend):
loop.run_forever() loop.run_forever()
def run(self): def run(self):
def server_thread(**kwargs):
self.app = self.webserver()
self.app.debug = False
self.app.run(**kwargs)
super().run() super().run()
os.putenv('FLASK_APP', 'platypush') os.putenv('FLASK_APP', 'platypush')
os.putenv('FLASK_ENV', 'production') os.putenv('FLASK_ENV', 'production')
kwargs = { kwargs = {
'host':'0.0.0.0', 'port':self.port, 'host':'0.0.0.0', 'port':self.port, 'use_reloader':False
'use_reloader':False, 'threaded':True,
} }
if self.ssl_context: if self.ssl_context:
@ -776,15 +767,18 @@ class HttpBackend(Backend):
self.logger.info('Initialized HTTP backend on port {}'.format(self.port)) self.logger.info('Initialized HTTP backend on port {}'.format(self.port))
self.webserver_thread = threading.Thread(target=server_thread(**kwargs), self.app = self.webserver()
name='WebServer') self.app.debug = False
self.webserver_thread.start() self.server_proc = Process(target=self.app.run,
name='WebServer',
kwargs=kwargs)
self.server_proc.start()
if not self.disable_websocket: if not self.disable_websocket:
self.websocket_thread = threading.Thread(target=self.websocket) self.websocket_thread = threading.Thread(target=self.websocket)
self.websocket_thread.start() self.websocket_thread.start()
self.webserver_thread.join() self.server_proc.join()
class HttpUtils(object): class HttpUtils(object):