forked from gf0842wf/gevent-server-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wserver_flask.py
53 lines (38 loc) · 1.19 KB
/
wserver_flask.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
# -*- coding: utf-8 -*-
"""WSGI Server Sample"""
import gevent
from gevent import socket
from gevent.pywsgi import WSGIServer
from gmysql import Pool
from flask import Flask
app = Flask(__name__)
app.config.update(dict(
DB = ('localhost', 'root', '112358', 'test')
)
)
pool = Pool(app.config["DB"], 20)
@app.route("/")
def hello():
rows = pool.fetchall("select * from book")
print rows
return str(rows)
@app.route("/sleep")
def sleep():
gevent.sleep(5)
return "awake..."
class WServerManager(gevent.Greenlet):
def __init__(self, port, app=None):
self.port = port
self.app = app
gevent.Greenlet.__init__(self)
def _app(self, environ, start_response):
start_response('200 OK', [('Content-Type','text/plain')])
yield "{0}\n".format(socket.getaddrinfo('www.baidu.com', 80))
def _run(self):
print("WSGI Server Listen at port {0}".format(self.port))
server = WSGIServer(('0.0.0.0', self.port), self.app or self._app)
server.serve_forever()
if __name__ == "__main__":
ws = WServerManager(7001, app)
ws.start()
gevent.wait()