-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmtcp.py
executable file
·45 lines (37 loc) · 1.06 KB
/
mtcp.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
"""
multiple thread, connection pool
"""
import time
from random import uniform
from time import sleep
import threading
import mysql.connector
from mysql.connector.connection import MySQLConnection
from mysql.connector.pooling import MySQLConnectionPool as mcp
from mysql.connector import Error
CON_POOL = mcp(pool_name="mypool", pool_size=32, pool_reset_session=True,
host='localhost', database='users', user='root', password='123456')
def read_user_from_db():
"""
read user info from db
"""
sleep(uniform(0, 1))
while True:
try:
conn = CON_POOL.get_connection()
cursor = conn.cursor()
cursor.execute("select * from userinfo")
res = cursor.fetchall()
conn.close()
return
except Error:
pass
if __name__ == "__main__":
threads = []
for i in xrange(1000):
t = threading.Thread(target=read_user_from_db)
t.daemon = True
t.start()
threads.append(t)
for thread in threads:
thread.join()