-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
72 lines (57 loc) · 2.14 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""main application file for jiminy-recommender"""
import caches
import flask
from logger import get_logger
import multiprocessing as mp
import predictions
import threading as t
import views
def main():
"""start the http service"""
# acquire logger
logger = get_logger()
# create the flask app object
app = flask.Flask(__name__)
# change this value for production environments
app.config['SECRET_KEY'] = 'secret!'
# queues for ipc with the prediction process
request_q = mp.Queue()
response_q = mp.Queue()
# start the prediction process
process = mp.Process(target=predictions.loop,
args=(request_q, response_q))
process.start()
# waiting for processing loop to become active
response_q.get()
# initialize a cache store
# (uses environment variables for connection information)
logger.debug("Initializing cache")
storage = caches.factory()
# create and start the cache updater thread
thread = t.Thread(target=caches.updater, args=(response_q, storage))
thread.start()
# configure routes and start the service
app.add_url_rule('/', view_func=views.ServerInfo.as_view('server'))
app.add_url_rule('/predictions/ratings',
view_func=views.PredictionsRatings.as_view(
'rating_prediction',
storage,
request_q))
app.add_url_rule('/predictions/ratings/<string:p_id>',
view_func=views.PredictionDetail.as_view(
'rating_predictions',
storage))
app.add_url_rule('/predictions/ranks',
view_func=views.PredictionsRanks.as_view(
'rank_prediction',
storage,
request_q))
app.add_url_rule('/predictions/ranks/<string:p_id>',
view_func=views.PredictionDetail.as_view(
'rank_predictions',
storage))
app.run(host='0.0.0.0', port=8080)
request_q.put('stop')
response_q.put('stop')
if __name__ == '__main__':
main()