forked from invinst/CPDBv2_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgunicorn.conf
44 lines (33 loc) · 1.21 KB
/
gunicorn.conf
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
import cProfile
import pstats
import io
import logging
import os
import time
for k,v in os.environ.items():
if k.startswith("GUNICORN_"):
key = k.split('_', 1)[1].lower()
locals()[key] = v
PROFILE_LIMIT = int(os.environ.get("PROFILE_LIMIT", 30))
PROFILER = bool(int(os.environ.get("PROFILER", 0)))
def profiler_enable(worker, req):
worker.profile = cProfile.Profile()
worker.profile.enable()
worker.log.info("PROFILING %d: %s" % (worker.pid, req.uri))
def profiler_summary(worker, req):
s = io.StringIO()
worker.profile.disable()
ps = pstats.Stats(worker.profile, stream=s).sort_stats('time', 'cumulative')
ps.print_stats(PROFILE_LIMIT)
logging.error("\n[%d] [INFO] [%s] URI %s" % (worker.pid, req.method, req.uri))
logging.error("[%d] [INFO] %s" % (worker.pid, unicode(s.getvalue())))
def pre_request(worker, req):
worker.start_time = time.time()
if PROFILER is True:
profiler_enable(worker, req)
def post_request(worker, req, *args):
total_time = time.time() - worker.start_time
logging.error("\n[%d] [INFO] [%s] Load Time: %.3fs\n" % (
worker.pid, req.method, total_time))
if PROFILER is True:
profiler_summary(worker, req)