Move to gevent production http server

This commit is contained in:
Tanner Collin 2019-11-08 02:37:57 +00:00
parent 6826f731c7
commit 38b5f2dbeb

View File

@ -17,6 +17,12 @@ from flask import abort, Flask, request, render_template
from werkzeug.exceptions import NotFound from werkzeug.exceptions import NotFound
from flask_cors import CORS from flask_cors import CORS
import gevent
from gevent import monkey
from gevent.pywsgi import WSGIServer
monkey.patch_all()
archive.init() archive.init()
CACHE_LENGTH = 300 CACHE_LENGTH = 300
@ -99,11 +105,6 @@ def static_story(sid):
url=url, url=url,
description=description) description=description)
print('Starting Flask...')
web_thread = threading.Thread(target=flask_app.run, kwargs={'port': 33842})
web_thread.setDaemon(True)
web_thread.start()
def new_id(): def new_id():
nid = gen_rand_id() nid = gen_rand_id()
while nid in news_cache or archive.get_story(nid): while nid in news_cache or archive.get_story(nid):
@ -117,6 +118,11 @@ def remove_ref(old_ref):
old_id = news_ref_to_id.pop(old_ref) old_id = news_ref_to_id.pop(old_ref)
logging.info('Removed ref {} id {}.'.format(old_ref, old_id)) logging.info('Removed ref {} id {}.'.format(old_ref, old_id))
http_server = WSGIServer(('', 33842), flask_app)
def feed_thread():
global news_index
try: try:
while True: while True:
# onboard new stories # onboard new stories
@ -148,11 +154,21 @@ try:
else: else:
remove_ref(update_ref) remove_ref(update_ref)
time.sleep(3) gevent.sleep(3)
news_index += 1 news_index += 1
if news_index == CACHE_LENGTH: news_index = 0 if news_index == CACHE_LENGTH: news_index = 0
except BaseException as e:
logging.error('feed_thread error: {} {}'.format(e.__class__.__name__, e))
http_server.stop()
print('Starting Feed thread...')
gevent.spawn(feed_thread)
print('Starting HTTP thread...')
try:
http_server.serve_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info('Exiting...') logging.info('Exiting...')
finally: finally: