python 多线程串行和并行的实例
程序员文章站
2022-07-04 19:31:23
如下所示:
#coding=utf-8
import threading
import time
import cx_oracle
from pprint...
如下所示:
#coding=utf-8 import threading import time import cx_oracle from pprint import pprint import csv table_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 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 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 import threading import time import cx_oracle from pprint import pprint import csv table_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 此时变成串行,数据库连接也只有一个: sid serial# username prev_sql_id 1 421 1957 test 928r7khrtn4jd
以上这篇python 多线程串行和并行的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: Python中正则表达式的用法总结
下一篇: Java之变量