pymysql + gevent,为什么多协程查询需要更多时间?
一个查询使用时间:0.6毫秒 10个协程使用2.2ms,为什么每个查询使用时间超过0.6ms?
我的测试结果:
pool start
[0]used time:0.000674962997437
[1]used time:0.000821113586426
[5]used time:0.000817775726318
[2]used time:0.00109195709229
[4]used time:0.00116109848022
[8]used time:0.00116205215454
[9]used time:0.00126385688782
[3]used time:0.00168299674988
[7]used time:0.00165200233459
[6]used time:0.00182700157166
pool end
use time: 0.002352
我的桌子是:
CREATE TABLE `user_info` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`sex` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`,`name`)
) ENGINE=InnoDB AUTO_INCREMENT=814 DEFAULT CHARSET=utf8;
我的python脚本进行测试,它连接db,创建10个协程以查询sql, 总使用时间可能等于一个查询,但不等于:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import gevent
from gevent.pool import Pool
from gevent import monkey
monkey.patch_all()
#monkey.patch_socket()
import pymysql
import time
import sys
size = 10
conn_list = []
def init_conn():
for i in range(0, size):
db = pymysql.connect(host = '127.0.0.1', port=13357, passwd = 'admin@123', user = 'admin_123', db= 'test')
conn_list.append(db)
def deinit_conn():
for i in range(0, size):
conn_list[i].close()
def nbquery(fmt, index):
cursor = conn_list[index].cursor()
ts1 = time.time()
#sql = fmt % (sub_odr_id[index%10])
data = cursor.execute(fmt)
ts2 = time.time()
print('[%d]used time:%s' % (index, ts2 - ts1))
cursor.close()
init_conn()
# sqlc="SELECT SLEEP(0.001)"
sqld="select * from user_info where id=2 and name='John'"
start = time.time()
print('pool start')
pool = Pool()
for i in range(0, size):
pool.spawn(nbquery, (sqld), i)
pool.join()
print('pool end')
print('use time: %f' % (time.time() - start))
#deinit_conn()