时间:2022-04-02 10:19 作者:admin
如下所示:
#coding=utf-8import threadingimport timeimport cx_Oraclefrom pprint import pprintimport csvtable_name = "dbtest.csv"f = open(table_name + ".csv", "w") def exp01(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp01' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp02(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp02' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp03(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp03' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp04(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp04' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp05(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp05' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp06(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp06' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp07(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp07' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp08(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp08' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp09(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp09' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp10(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp10' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp11(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp11' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp12(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp12' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp13(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp13' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp14(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp14' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() threads = []t1 = threading.Thread(target=exp01,name='exp01')threads.append(t1)t2 = threading.Thread(target=exp02,name='exp02')threads.append(t2)t2 = threading.Thread(target=exp03,name='exp03')threads.append(t2)t2 = threading.Thread(target=exp04,name='exp04')threads.append(t2)t2 = threading.Thread(target=exp05,name='exp05')threads.append(t2)t2 = threading.Thread(target=exp06,name='exp06')threads.append(t2)t2 = threading.Thread(target=exp07,name='exp07')threads.append(t2)t2 = threading.Thread(target=exp08,name='exp08')threads.append(t2)t2 = threading.Thread(target=exp09,name='exp09')threads.append(t2)t2 = threading.Thread(target=exp10,name='exp10')threads.append(t2)t2 = threading.Thread(target=exp11,name='exp11')threads.append(t2)t2 = threading.Thread(target=exp12,name='exp12')threads.append(t2)t2 = threading.Thread(target=exp13,name='exp13')threads.append(t2)t2 = threading.Thread(target=exp14,name='exp14')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 " 输出:C:\python/' target='_blank'>python27\Python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p7.py<_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><_MainThread(MainThread, started 156528)><Thread(exp01, started 155004)>select 'exp01' from dual<Thread(exp12, started 155744)> select 'exp12' from dual<Thread(exp02, started 155000)> select 'exp02' from dual<Thread(exp08, started 155728)>select 'exp08' from dual<Thread(exp06, started 155752)>select 'exp06' from dual<Thread(exp03, started 154816)>select 'exp03' from dual<Thread(exp09, started 156544)>select 'exp09' from dual<Thread(exp11, started 155760)>select 'exp11' from dual<Thread(exp04, started 154112)>select 'exp04' from dual<Thread(exp10, started 155764)><Thread(exp05, started 154640)> select 'exp10' from dualselect 'exp05' from dual <Thread(exp07, started 155188)>select 'exp07' from dual<Thread(exp13, started 154600)>select 'exp13' from dual<Thread(exp14, started 155804)>select 'exp14' from dual SIDSERIAL#USERNAMEPREV_SQL_ID191179TEST01tjnxmmurdw721075TESTg5ph474nsjvwv312907TEST87mdhpgj9k5tz44191303TEST4g8r4bkf8aq3n5420655TEST1rx9mjdvp1udx64211955TEST928r7khrtn4jd742451TESTd6sgjjwpm74qz88393TESTa1hg7hrwgrdqk984043TESTfqjuj4qp5hmf010841111TEST2jzx0889h3k5n111252145TESTawtzfbx7dhn881212537TEST55tzs9gdmcd4p1312549TEST4v01fvb5sj7k4141255265TEST39bcsfjr5y62b 此时是并发执行 改变join的位置:#coding=utf-8import threadingimport timeimport cx_Oraclefrom pprint import pprintimport csvtable_name = "dbtest.csv"f = open(table_name + ".csv", "w") def exp01(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp01' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp02(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp02' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp03(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp03' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp04(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp04' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp05(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp05' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp06(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp06' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp07(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp07' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp08(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp08' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp09(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp09' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp10(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp10' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp11(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp11' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp12(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp12' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp13(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp13' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() def exp14(): conn = cx_Oracle.connect('test/test@192.168.137.2/serv') cursor = conn.cursor() owner = "system" writer = csv.writer(f, lineterminator="\n", quoting=csv.QUOTE_NONNUMERIC) tname = threading.current_thread() print tname exportOracleSql = "select 'exp14' from dual" print exportOracleSql x = cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() threads = []t1 = threading.Thread(target=exp01,name='exp01')threads.append(t1)t2 = threading.Thread(target=exp02,name='exp02')threads.append(t2)t2 = threading.Thread(target=exp03,name='exp03')threads.append(t2)t2 = threading.Thread(target=exp04,name='exp04')threads.append(t2)t2 = threading.Thread(target=exp05,name='exp05')threads.append(t2)t2 = threading.Thread(target=exp06,name='exp06')threads.append(t2)t2 = threading.Thread(target=exp07,name='exp07')threads.append(t2)t2 = threading.Thread(target=exp08,name='exp08')threads.append(t2)t2 = threading.Thread(target=exp09,name='exp09')threads.append(t2)t2 = threading.Thread(target=exp10,name='exp10')threads.append(t2)t2 = threading.Thread(target=exp11,name='exp11')threads.append(t2)t2 = threading.Thread(target=exp12,name='exp12')threads.append(t2)t2 = threading.Thread(target=exp13,name='exp13')threads.append(t2)t2 = threading.Thread(target=exp14,name='exp14')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 " C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p7.py<_MainThread(MainThread, started 154776)><Thread(exp01, started 156240)>select 'exp01' from dual 此时变成串行,数据库连接也只有一个: SIDSERIAL#USERNAMEPREV_SQL_ID14211957TEST928r7khrtn4jd
以上这篇python 多线程串行和并行的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
(责任编辑:admin)《中华人民共和国工业和信息化部》 编号:ICP备00012341号
Copyright © 2002 -2018 香港云主机 版权所有
声明:香港云主机品牌标志、品牌吉祥物均已注册商标,版权所有,窃用必究
云官方微信