-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
113 lines (90 loc) · 2.54 KB
/
service.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
# coding=utf-8
import SimpleHTTPServer
import threading
import SocketServer
import datetime
import urllib
import json
from core.term_test import TermTestManage
from core.flow import Flow
#import chronic
#from pprint import pprint
#import hadoop.hadoopget
DATE_FORMAT = '%Y-%m-%d'
TERM_FILE = 'data/term_list.txt'
DIM_FILE = 'data/dim_list.txt'
DB_NAME = 'RTBApp'
manage=None
f=None
lock = threading.Lock()
def parse_param(s):
param = {}
content = s.split('&')
for text in content:
print text
key, value = text.split('=')
param[key] = value
return param
def exec_result(length, data):
value_list = []
value = f.total_current()
#with chronic.Timer('exec_result'):
for i in range(length):
value_list.append(str(manage.estimate(term_map=data, total=value)))
value = f.future(value, i)
# 计算单次执行时间
#pprint(chronic.timings)
#print ','.join(value_list)
return ','.join(value_list)
def load():
global manage, f
# 锁定
lock.acquire()
manage = TermTestManage()
now = datetime.datetime.now()
day = now.strftime(DATE_FORMAT)
f = Flow(day, DB_NAME)
# 加载基础维度
fo = open(DIM_FILE, 'r')
lines = fo.readlines()
manage.load_dim(lines)
fo.close()
# 加载修正组合
fo = open(TERM_FILE, 'r')
lines = fo.readlines()
manage.load(lines)
fo.close()
#释放
lock.release()
class HTTPHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
return
def do_POST(self):
print "do_POST: thread",threading.current_thread().getName()
length = int(self.headers.getheader('content-length'))
content = urllib.unquote(self.rfile.read(length))
# param = parse_param(content)
param = json.loads(content)
#param['data'] = json.loads(param['data'])
print param
if 'reload' in param:
load()
return
if 'data' not in param:
return
elif 'length' in param:
self.wfile.write(exec_result(param['length'], param['data']))
#self.wfile.write("response")
return
else:
self.wfile.write(exec_result(1, param['data']))
return
if __name__ == '__main__':
PORT = 9112
httpd = SocketServer.TCPServer(("", PORT), HTTPHandler)
print "serving at port", PORT
load()
print "==========load finish=========="
print "MAIN: thread",threading.current_thread().getName()
httpd.serve_forever()