#coding=utf-8
import threading
import time
import cx_Oracle
from pprint import pprint
import csv
print time.asctime()
table_name = "dbtest.csv"
f = open(table_name + ".csv", "w")
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
def query01():
tname = threading.current_thread()
aa=10
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
def query02():
tname = threading.current_thread()
aa=20
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
def query03():
tname = threading.current_thread()
aa=30
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
def query04():
tname = threading.current_thread()
aa=40
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
def query05():
tname = threading.current_thread()
aa=50
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
def query06():
tname = threading.current_thread()
aa=60
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
def query07():
tname = threading.current_thread()
aa=70
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
return row
cursor.close()
def query08():
tname = threading.current_thread()
aa=80
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
def query09():
tname = threading.current_thread()
aa=90
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
def query10():
tname = threading.current_thread()
aa=100
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
threads = []
t1 = threading.Thread(target=query01, name='query01')
threads.append(t1)
t2 = threading.Thread(target=query02, name='query02')
threads.append(t2)
t2 = threading.Thread(target=query03, name='query03')
threads.append(t2)
t2 = threading.Thread(target=query04, name='query04')
threads.append(t2)
t2 = threading.Thread(target=query05, name='query05')
threads.append(t2)
t2 = threading.Thread(target=query06, name='query06')
threads.append(t2)
t2 = threading.Thread(target=query07, name='query07')
threads.append(t2)
t2 = threading.Thread(target=query08, name='query08')
threads.append(t2)
t2 = threading.Thread(target=query09, name='query09')
threads.append(t2)
t2 = threading.Thread(target=query10, name='query10')
threads.append(t2)
if __name__ == '__main__':
for t in threads:
# t.setDaemon(True)
t.start()
# t.run()
# t.start()
# print '3333333'
print threading.current_thread()
# print t.is_alive()
# print '3333333'
t.join()
print "all over "
print time.asctime()
C:Python27python.exe C:/Users/tlcb/PycharmProjects/untitled/a2.py
Wed Mar 28 11:08:19 2018
<_MainThread(MainThread, started 18744)>
[(10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10'), (10, '10sssssssss10', 'tlcb', 'tlcb'), (10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10')]
<_MainThread(MainThread, started 18744)>
[(20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20')]
<_MainThread(MainThread, started 18744)>
[(30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30')]
<_MainThread(MainThread, started 18744)>
[(40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40')]
<_MainThread(MainThread, started 18744)>
[(50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50')]
<_MainThread(MainThread, started 18744)>
[(60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60')]
<_MainThread(MainThread, started 18744)>
<_MainThread(MainThread, started 18744)>
[(80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80')]
<_MainThread(MainThread, started 18744)>
[(90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90')]
<_MainThread(MainThread, started 18744)>
[(100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100')]
all over
Wed Mar 28 11:08:34 2018
Process finished with exit code 0
这个时候是串行 花费了15秒
多线程跑:
#coding=utf-8
import threading
import time
import cx_Oracle
from pprint import pprint
import csv
print time.asctime()
table_name = "dbtest.csv"
f = open(table_name + ".csv", "w")
def query01():
tname = threading.current_thread()
aa=10
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query02():
tname = threading.current_thread()
aa=20
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query03():
tname = threading.current_thread()
aa=30
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query04():
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
tname = threading.current_thread()
aa=40
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query05():
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
tname = threading.current_thread()
aa=50
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query06():
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
tname = threading.current_thread()
aa=60
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query07():
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
tname = threading.current_thread()
aa=70
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
return row
cursor.close()
conn.close()
def query08():
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
tname = threading.current_thread()
aa=80
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query09():
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
tname = threading.current_thread()
aa=90
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
def query10():
conn = cx_Oracle.connect('tlcbuser/tlcbuser@20.5.101.31/tlyy')
cursor = conn.cursor()
tname = threading.current_thread()
aa=100
# cursor.execute("SELECT * FROM TEST100 WHERE ID = %s", [aa])
cursor.prepare("""SELECT * FROM TEST100 WHERE ID=:id""")
cursor.execute(None,{'id':aa})
row = cursor.fetchall()
print row
return row
cursor.close()
conn.close()
threads = []
t1 = threading.Thread(target=query01, name='query01')
threads.append(t1)
t2 = threading.Thread(target=query02, name='query02')
threads.append(t2)
t2 = threading.Thread(target=query03, name='query03')
threads.append(t2)
t2 = threading.Thread(target=query04, name='query04')
threads.append(t2)
t2 = threading.Thread(target=query05, name='query05')
threads.append(t2)
t2 = threading.Thread(target=query06, name='query06')
threads.append(t2)
t2 = threading.Thread(target=query07, name='query07')
threads.append(t2)
t2 = threading.Thread(target=query08, name='query08')
threads.append(t2)
t2 = threading.Thread(target=query09, name='query09')
threads.append(t2)
t2 = threading.Thread(target=query10, name='query10')
threads.append(t2)
if __name__ == '__main__':
for t in threads:
# t.setDaemon(True)
t.start()
# t.run()
# t.start()
# print '3333333'
print threading.current_thread()
# print t.is_alive()
# print '3333333'
t.join()
print "all over "
print time.asctime()
C:Python27python.exe C:/Users/tlcb/PycharmProjects/untitled/a2.py
Wed Mar 28 11:12:47 2018
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
<_MainThread(MainThread, started 22500)>
[(40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40'), (40, '40boobook40', '40aaaaaaaaaaaa40', '40bbbbbbbbbbbbbbbbb40')]
[(60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60'), (60, '60boobook60', '60aaaaaaaaaaaa60', '60bbbbbbbbbbbbbbbbb60')]
[(80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80'), (80, '80boobook80', '80aaaaaaaaaaaa80', '80bbbbbbbbbbbbbbbbb80')]
[(50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50'), (50, '50boobook50', '50aaaaaaaaaaaa50', '50bbbbbbbbbbbbbbbbb50')]
[(10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10'), (10, '10sssssssss10', 'tlcb', 'tlcb'), (10, '10boobook10', '10aaaaaaaaaaaa10', '10bbbbbbbbbbbbbbbbb10')]
[(20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20'), (20, '20boobook20', '20aaaaaaaaaaaa20', '20bbbbbbbbbbbbbbbbb20')]
[(30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30'), (30, '30boobook30', '30aaaaaaaaaaaa30', '30bbbbbbbbbbbbbbbbb30')]
[(100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100'), (100, '100boobook100', '100aaaaaaaaaaaa100', '100bbbbbbbbbbbbbbbbb100')]
[(90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90'), (90, '90boobook90', '90aaaaaaaaaaaa90', '90bbbbbbbbbbbbbbbbb90')]
all over
Wed Mar 28 11:12:55 2018
Process finished with exit code 0
此时花了 8秒