Skip to content

Commit cac44d4

Browse files
committed
Add waiter samples.
1 parent 7e15cf8 commit cac44d4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

samples/waiter_gevent.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import print_function
2+
"""Demo using Gevent with mysqlclient."""
3+
4+
import gevent.hub
5+
import select
6+
import MySQLdb
7+
8+
9+
def gevent_waiter(fd, hub=gevent.hub.get_hub()):
10+
hub.wait(hub.loop.io(fd, 1))
11+
12+
13+
def f(n):
14+
conn = MySQLdb.connect(user='root', waiter=gevent_waiter)
15+
cur = conn.cursor()
16+
cur.execute("SELECT SLEEP(%s)", (n,))
17+
cur.execute("SELECT 1+%s", (n,))
18+
print(cur.fetchall()[0])
19+
20+
21+
gevent.spawn(f, 1)
22+
gevent.spawn(f, 2)
23+
gevent.spawn(f, 3)
24+
gevent.spawn(f, 4)
25+
gevent.sleep(5)

samples/waiter_meinheld.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import meinheld
2+
import MySQLdb
3+
4+
5+
def meinheld_waiter(fd):
6+
meinheld.server.trampoline(fd, read=True, timeout=10)
7+
8+
9+
def app(env, start):
10+
cont = b"Hello, World\n"
11+
conn = MySQLdb.connect(user="root", waiter=meinheld_waiter)
12+
cur = conn.cursor()
13+
cur.execute("SELECT SLEEP(2)")
14+
start(b"200 OK", [('Content-Type', 'text/plain'), ('Content-Length', str(len(cont)))])
15+
return [cont]
16+
17+
18+
meinheld.server.listen(("0.0.0.0", 8080))
19+
meinheld.server.run(app)

0 commit comments

Comments
 (0)