python 多线程串行和并行的实例


Posted in Python onFebruary 22, 2019

如下所示:

#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 相关文章推荐
python del()函数用法
Mar 24 Python
Python基础中所出现的异常报错总结
Nov 19 Python
Python入门_浅谈数据结构的4种基本类型
May 16 Python
Python生成数字图片代码分享
Oct 31 Python
Python numpy.array()生成相同元素数组的示例
Nov 12 Python
django重新生成数据库中的某张表方法
Aug 28 Python
Python @property使用方法解析
Sep 17 Python
Anaconda之conda常用命令介绍(安装、更新、删除)
Oct 06 Python
使用jupyter notebook将文件保存为Markdown,HTML等文件格式
Apr 14 Python
在Python中字典按值排序的实现方法
Nov 12 Python
Python爬虫回测股票的实例讲解
Jan 22 Python
图神经网络GNN算法
May 11 Python
Python OOP类中的几种函数或方法总结
Feb 22 #Python
VSCode Python开发环境配置的详细步骤
Feb 22 #Python
python中logging模块的一些简单用法的使用
Feb 22 #Python
Python 限制线程的最大数量的方法(Semaphore)
Feb 22 #Python
Python3.6.x中内置函数总结及讲解
Feb 22 #Python
python自定义线程池控制线程数量的示例
Feb 22 #Python
Python实现多进程的四种方式
Feb 22 #Python
You might like
深入PHP异步执行的详解
2013/06/03 PHP
php中实现记住密码下次自动登录的例子
2014/11/06 PHP
PHP基于关联数组20行代码搞定约瑟夫问题示例
2017/11/07 PHP
PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例
2019/06/17 PHP
js停止输出代码
2008/07/20 Javascript
jQuery参数列表集合
2011/04/06 Javascript
jQuery源码解读之hasClass()方法分析
2015/02/20 Javascript
JavaScript Length 属性的总结
2015/11/02 Javascript
javascript仿百度输入框提示自动下拉补全
2016/01/07 Javascript
JavaScript定义数组的三种方法(new Array(),new Array('x','y')
2016/10/04 Javascript
javascript中json基础知识详解
2017/01/19 Javascript
详解EasyUi控件中的Datagrid
2017/08/23 Javascript
JavaScript 作用域实例分析
2019/10/02 Javascript
Vue快速实现通用表单验证功能
2019/12/05 Javascript
VSCode 配置uni-app的方法
2020/07/11 Javascript
vue实现图书管理系统
2020/12/29 Vue.js
Python入门篇之对象类型
2014/10/17 Python
go语言计算两个时间的时间差方法
2015/03/13 Python
浅谈python抛出异常、自定义异常, 传递异常
2016/06/20 Python
python统计中文字符数量的两种方法
2019/01/31 Python
Python企业编码生成系统总体系统设计概述
2019/07/26 Python
python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】
2019/10/24 Python
python使用yield压平嵌套字典的超简单方法
2019/11/02 Python
使用python脚本自动生成K8S-YAML的方法示例
2020/07/12 Python
拉斯维加斯城市观光通行证:Las Vegas Pass
2019/05/21 全球购物
心理健康课教学反思
2014/02/13 职场文书
正科级干部考察材料
2014/05/29 职场文书
奥运会口号
2014/06/13 职场文书
质量保证书格式模板
2015/02/27 职场文书
实习推荐信格式模板
2015/03/27 职场文书
校园运动会广播稿
2015/08/19 职场文书
中职班主任培训心得体会
2016/01/07 职场文书
退休劳动合同怎么写?
2019/10/25 职场文书
《风不能把阳光打败》读后感3篇
2020/01/06 职场文书
正确的理解和使用Django信号(Signals)
2021/04/14 Python
python字典的元素访问实例详解
2021/07/21 Python