Skip to content

Commit 421ffe5

Browse files
committed
Update readme
1 parent 87271b7 commit 421ffe5

2 files changed

Lines changed: 59 additions & 8 deletions

File tree

README.rst

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,69 @@ This package contains a fork of PyMySQL supporting Tornado.
1212
Example
1313
-------
1414

15-
Simple
15+
example
1616
~~~~~~~
1717

18-
.. include:: example.py
19-
:code: python
18+
::
2019

21-
pool
22-
~~~~~
20+
#!/usr/bin/env python
21+
from __future__ import print_function
22+
23+
from tornado import ioloop, gen
24+
import tornado_mysql
25+
26+
@gen.coroutine
27+
def main():
28+
conn = yield tornado_mysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='mysql')
29+
cur = conn.cursor()
30+
yield cur.execute("SELECT Host,User FROM user")
31+
print(cur.description)
32+
for row in cur:
33+
print(row)
34+
cur.close()
35+
conn.close()
36+
37+
ioloop.IOLoop.current().run_sync(main)
38+
39+
example_pool
40+
~~~~~~~~~~~~
41+
42+
::
43+
44+
#!/usr/bin/env python
45+
from __future__ import print_function
46+
47+
from tornado import ioloop, gen
48+
from tornado_mysql import pools
49+
50+
51+
pools.DEBUG = True
52+
53+
54+
POOL = pools.Pool(
55+
dict(host='127.0.0.1', port=3306, user='root', passwd='', db='mysql'),
56+
max_idle_connections=1,
57+
max_recycle_sec=3)
58+
59+
60+
@gen.coroutine
61+
def worker(n):
62+
for i in range(10):
63+
t = 1
64+
print(n, "sleeping", t, "seconds")
65+
cur = yield POOL.execute("SELECT SLEEP(%s)", (t,))
66+
print(n, cur.fetchall())
67+
68+
69+
@gen.coroutine
70+
def main():
71+
workers = [worker(i) for i in range(10)]
72+
yield workers
73+
74+
75+
ioloop.IOLoop.current().run_sync(main)
76+
print(POOL._opened_conns)
2377

24-
.. include:: example_pool.py
25-
:code: python
2678

2779
Requirements
2880
-------------

example_pool.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python
22
from __future__ import print_function
3-
import random
43

54
from tornado import ioloop, gen
65
from tornado_mysql import pools

0 commit comments

Comments
 (0)