python 多线程串行和并行的实例
如下所示:
#coding=utf-8 importthreading importtime importcx_Oracle frompprintimportpprint importcsv table_name="dbtest.csv" f=open(table_name+".csv","w") defexp01(): 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() printtname exportOracleSql="select'exp01'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp02(): 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() printtname exportOracleSql="select'exp02'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp03(): 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() printtname exportOracleSql="select'exp03'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp04(): 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() printtname exportOracleSql="select'exp04'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp05(): 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() printtname exportOracleSql="select'exp05'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp06(): 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() printtname exportOracleSql="select'exp06'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp07(): 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() printtname exportOracleSql="select'exp07'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp08(): 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() printtname exportOracleSql="select'exp08'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp09(): 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() printtname exportOracleSql="select'exp09'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp10(): 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() printtname exportOracleSql="select'exp10'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp11(): 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() printtname exportOracleSql="select'exp11'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp12(): 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() printtname exportOracleSql="select'exp12'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp13(): 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() printtname exportOracleSql="select'exp13'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp14(): 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() printtname exportOracleSql="select'exp14'fromdual" printexportOracleSql 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__': fortinthreads: #t.setDaemon(True) t.start() #t.run() #t.start() #print'3333333' printthreading.current_thread() #printt.is_alive() #print'3333333' t.join() print"allover" 输出: C:\Python27\python.exeC:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p7.py <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)> <_MainThread(MainThread,started156528)>select'exp01'fromdual select'exp12'fromdual select'exp02'fromdual select'exp08'fromdual select'exp06'fromdual select'exp03'fromdual select'exp09'fromdual select'exp11'fromdual select'exp04'fromdual select'exp10'fromdualselect'exp05'fromdual select'exp07'fromdual select'exp13'fromdual select'exp14'fromdual SID SERIAL# USERNAME PREV_SQL_ID 1 9 1179 TEST 01tjnxmmurdw7 2 10 75 TEST g5ph474nsjvwv 3 12 907 TEST 87mdhpgj9k5tz 4 419 1303 TEST 4g8r4bkf8aq3n 5 420 655 TEST 1rx9mjdvp1udx 6 421 1955 TEST 928r7khrtn4jd 7 424 51 TEST d6sgjjwpm74qz 8 839 3 TEST a1hg7hrwgrdqk 9 840 43 TEST fqjuj4qp5hmf0 10 841 111 TEST 2jzx0889h3k5n 11 1252 145 TEST awtzfbx7dhn88 12 1253 7 TEST 55tzs9gdmcd4p 13 1254 9 TEST 4v01fvb5sj7k4 14 1255 265 TEST 39bcsfjr5y62b 此时是并发执行 改变join的位置: #coding=utf-8 importthreading importtime importcx_Oracle frompprintimportpprint importcsv table_name="dbtest.csv" f=open(table_name+".csv","w") defexp01(): 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() printtname exportOracleSql="select'exp01'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp02(): 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() printtname exportOracleSql="select'exp02'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp03(): 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() printtname exportOracleSql="select'exp03'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp04(): 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() printtname exportOracleSql="select'exp04'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp05(): 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() printtname exportOracleSql="select'exp05'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp06(): 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() printtname exportOracleSql="select'exp06'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp07(): 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() printtname exportOracleSql="select'exp07'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp08(): 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() printtname exportOracleSql="select'exp08'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp09(): 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() printtname exportOracleSql="select'exp09'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp10(): 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() printtname exportOracleSql="select'exp10'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp11(): 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() printtname exportOracleSql="select'exp11'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp12(): 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() printtname exportOracleSql="select'exp12'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp13(): 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() printtname exportOracleSql="select'exp13'fromdual" printexportOracleSql x=cursor.execute(exportOracleSql) time.sleep(1000) cursor.close() conn.close() defexp14(): 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() printtname exportOracleSql="select'exp14'fromdual" printexportOracleSql 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__': fortinthreads: #t.setDaemon(True) t.start() #t.run() #t.start() #print'3333333' printthreading.current_thread() #printt.is_alive() #print'3333333' t.join() print"allover" C:\Python27\python.exeC:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p7.py <_MainThread(MainThread,started154776)> select'exp01'fromdual 此时变成串行,数据库连接也只有一个: SID SERIAL# USERNAME PREV_SQL_ID 1 421 1957 TEST 928r7khrtn4jd
以上这篇python多线程串行和并行的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。