-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.py
84 lines (61 loc) · 1.82 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
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from flask import Flask,jsonify
from werkzeug import cached_property
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
from gevent.monkey import patch_all
patch_all()
from psycogreen.gevent import patch_psycopg
patch_psycopg()
app = Flask(__name__)
app.config.from_pyfile('config.py')
db = SQLAlchemy(app)
db.engine.pool._use_threadlocal = True
class AsyndbQuery(BaseQuery):
'''Provide all kinds of query functions.'''
def jsonify(self):
'''Converted datas into JSON.'''
for item in self.all():
yield item.as_dict
class Ayndb(db.Model):
query_class = AsyndbQuery
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(60))
done = db.Column(db.Boolean)
priority = db.Column(db.Integer)
@cached_property
def as_dict(self):
return {
'id': self.id,
'title': self.title,
'done': self.done,
'priority': self.priority
}
@app.route('/test/postgres/')
def sleep_postgres():
db.session.execute('SELECT pg_sleep(5)')
return jsonify(data = list(Ayndb.query.jsonify()))
def create_data():
""" A helper function to create our tables and some Todo objects.
"""
db.create_all()
alldata = []
for i in range(50):
item = Ayndb(
title="test for postgres,this is {0}".format(i),
done=(i % 2 == 0),
priority=(i % 5)
)
alldata.append(item)
db.session.add_all(alldata)
db.session.commit()
db.session.close()
if __name__ == '__main__':
if '-c' in sys.argv:
create_data()
else:
#app.run()
from gevent.pywsgi import WSGIServer
http_server = WSGIServer(('', 8080), app)
http_server.serve_forever()