def goodquery(sql):
db = pymysql.connect(host = 'localhost', passwd = '123123', user = 'root', db= 'rui')
cursor = db.cursor()
data = cursor.execute(sql)
cursor.close()
db.close()
return cursor
sqla='select count(*) from kkk'
sqlb="select * from kkk where name like '%888888%'"
jobs = [gevent.spawn(goodquery, (sqla)),gevent.spawn(goodquery, (sqlb))]
#jobs = [gevent.spawn(goodquery, (sqla)),gevent.spawn(goodquery,(sqlb)),gevent.spawn(goodquery,(sqlb))]
gevent.joinall(jobs, timeout=2)
what_you_want = [job.value for job in jobs]
print what_you_want
for i in what_you_want:
for a in i:
print a
import pymysql
import threading
def goodquery(sql):
db = pymysql.connect(host = 'localhost', passwd = '123123', user = 'root', db= 'rui')
cursor = db.cursor()
data = cursor.execute(sql)
cursor.close()
db.close()
print cursor
return cursor
sqla='select count(*) from kkk'
sqlb="select * from kkk where name like '%888888%'"
#jobs = [gevent.spawn(goodquery,(sqla)),gevent.spawn(goodquery,(sqlb)),gevent.spawn(goodquery,(sqlb)),gevent.spawn(goodquery,(sqlb)),gevent.spawn(goodquery,(sqlb))]
#jobs = [gevent.spawn(goodquery, (sqla)),gevent.spawn(goodquery,(sqlb)),gevent.spawn(goodquery,(sqlb))]
#gevent.joinall(jobs, timeout=30)
#what_you_want = [job.value for job in jobs]
threads=[]
for i in range(5):
threads.append( threading.Thread( target=goodquery,args=(sqlb,) ) )
for t in threads:
t.start()
for t in threads:
t.join()
他的测试结果要比gevent慢点 ~但也是并发的执行,可以在mysql进程里面看到执行的记录 ~
[root@101 ~]# time python t.py
real 0m11.122s
user 0m0.095s
sys 0m0.026s<span style="background-color: rgb(255, 255, 255);"> </span>