Move to gevent production http server
This commit is contained in:
parent
6826f731c7
commit
38b5f2dbeb
|
@ -17,6 +17,12 @@ from flask import abort, Flask, request, render_template
|
|||
from werkzeug.exceptions import NotFound
|
||||
from flask_cors import CORS
|
||||
|
||||
import gevent
|
||||
from gevent import monkey
|
||||
from gevent.pywsgi import WSGIServer
|
||||
|
||||
monkey.patch_all()
|
||||
|
||||
archive.init()
|
||||
|
||||
CACHE_LENGTH = 300
|
||||
|
@ -99,11 +105,6 @@ def static_story(sid):
|
|||
url=url,
|
||||
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():
|
||||
nid = gen_rand_id()
|
||||
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)
|
||||
logging.info('Removed ref {} id {}.'.format(old_ref, old_id))
|
||||
|
||||
http_server = WSGIServer(('', 33842), flask_app)
|
||||
|
||||
def feed_thread():
|
||||
global news_index
|
||||
|
||||
try:
|
||||
while True:
|
||||
# onboard new stories
|
||||
|
@ -148,11 +154,21 @@ try:
|
|||
else:
|
||||
remove_ref(update_ref)
|
||||
|
||||
time.sleep(3)
|
||||
gevent.sleep(3)
|
||||
|
||||
news_index += 1
|
||||
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:
|
||||
logging.info('Exiting...')
|
||||
finally:
|
||||
|
|
Loading…
Reference in New Issue
Block a user