中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python 多線程串行和并行的實例

發布時間:2020-10-06 12:58:54 來源:腳本之家 閱讀:154 作者:zhaoyangjian724 欄目:開發技術

如下所示:

#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	g5ph574nsjvwv
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	2jzx0889h4k5n
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 多線程串行和并行的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

合川市| 南京市| 衡阳县| 岳池县| 湖北省| 明溪县| 石门县| 清水县| 巴塘县| 靖江市| 安多县| 宿州市| 普格县| 山阳县| 乌兰浩特市| 咸宁市| 库尔勒市| 女性| 清徐县| 宜兴市| 呼伦贝尔市| 花莲县| 灵寿县| 三江| 雷州市| 茌平县| 克东县| 开鲁县| 时尚| 南澳县| 绍兴市| 民和| 丹凤县| 水富县| 林西县| 仁化县| 阳城县| 黄山市| 新宁县| 衢州市| 弥渡县|