python 多線程串行和并行的實(shí)例
如下所示:
#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
此時(shí)是并發(fā)執(zhí)行
改變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
此時(shí)變成串行,數(shù)據(jù)庫(kù)連接也只有一個(gè):
SID SERIAL# USERNAME PREV_SQL_ID
1 421 1957 TEST 928r7khrtn4jd
以上這篇python 多線程串行和并行的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python爬蟲(chóng)爬取幽默笑話(huà)網(wǎng)站
這篇文章主要介紹了python爬蟲(chóng)爬取幽默笑話(huà)網(wǎng)站,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
python numpy數(shù)組的索引和切片的操作方法
NumPy 是一個(gè) Python 包。 它代表 “Numeric Python”。它是一個(gè)由多維數(shù)組對(duì)象和用于處理數(shù)組的例程集合組成的庫(kù)。這篇文章主要介紹了python numpy 數(shù)組的索引和切片,需要的朋友可以參考下2018-10-10
Python入門(mén)教程(三十二)Python的命令行輸入
這篇文章主要介紹了Python入門(mén)教程(三十二)Python的命令行輸入,Python是一門(mén)非常強(qiáng)大好用的語(yǔ)言,也有著易上手的特性,本文為入門(mén)教程,需要的朋友可以參考下2023-05-05
Python3日期與時(shí)間戳轉(zhuǎn)換的幾種方法詳解
我們可以利用內(nèi)置模塊 datetime 獲取當(dāng)前時(shí)間,然后將其轉(zhuǎn)換為對(duì)應(yīng)的時(shí)間戳。這篇文章主要介紹了Python3日期與時(shí)間戳轉(zhuǎn)換的幾種方法,需要的朋友可以參考下2019-06-06
手把手教你實(shí)現(xiàn)Python連接數(shù)據(jù)庫(kù)并快速取數(shù)的工具
在數(shù)據(jù)生產(chǎn)應(yīng)用部門(mén),取數(shù)分析是一個(gè)很常見(jiàn)的需求,實(shí)際上業(yè)務(wù)人員需求時(shí)刻變化,最高效的方式是讓業(yè)務(wù)部門(mén)自己來(lái)取。本文就來(lái)手把手教大家搭建一個(gè)?Python?連接數(shù)據(jù)庫(kù),快速取數(shù)工具,需要的可以參考一下2022-11-11
跟老齊學(xué)Python之關(guān)于類(lèi)的初步認(rèn)識(shí)
這篇文章主要介紹了Python中關(guān)于類(lèi)的一些術(shù)語(yǔ)解釋?zhuān)m然有些枯燥,但是要了解類(lèi)的話(huà),這些內(nèi)容是必須的2014-10-10
python單例模式的應(yīng)用場(chǎng)景實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于python單例模式的應(yīng)用場(chǎng)景實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-02-02
Python爬蟲(chóng)必備之XPath解析庫(kù)
今天給大家?guī)?lái)的是Python爬蟲(chóng)的相關(guān)知識(shí),文章圍繞著XPath解析庫(kù)展開(kāi),文中有非常詳細(xì)的代碼示例及介紹,需要的朋友可以參考下2021-06-06

