python 多线程与多进程(python 多线程串行和并行的实例)
类别:脚本大全 浏览量:2988
时间:2022-01-26 01:12:29 python 多线程与多进程
python 多线程串行和并行的实例如下所示:
|
#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 多线程串行和并行的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/zhaoyangjian724/article/details/77910808
您可能感兴趣
- python接口管理系统(基于Python实现用户管理系统)
- python最火开源项目(5月份Github上Python开源项目排行)
- python大数据学习顺序(Python如何处理大数据?3个技巧效率提升攻略推荐)
- 协程在python中怎么使用(python协程之动态添加任务的方法)
- python外部如何调嵌套函数(python中嵌套函数的实操步骤)
- python excel 合并表格(Python实现合并excel表格的方法分析)
- python 文本文件读取方法(Python逐行读取文件中内容的简单方法)
- python删除列表中的重复元素(Python实现去除列表中重复元素的方法总结7种方法)
- python3函数的使用方法(Python3.5基础之函数的定义与使用实例详解参数、作用域、递归、重载等)
- python真的能高效处理excel报表吗(Python数据报表之Excel操作模块用法分析)
- python selenium用法详解(python selenium执行所有测试用例并生成报告的方法)
- python的os模块操作(Python OS模块实例详解)
- python创建进程的方法(Python多进程fork函数详解)
- python2个字典合成(Python两个字典键同值相加的几种方法)
- python获取当前时间戳(Python基于datetime或time模块分别获取当前时间戳的方法实例)
- python解析csv文件存到数据库(python读取csv和txt数据转换成向量的实例)
- 阴生植物为什么不怕照不到阳光(阴生植物为什么不怕照不到阳光)
- 阴生环境 耐阴地被植物,你知道哪些(阴生环境耐阴地被植物)
- 常见的喜阴植物有哪些 养室内盆栽就在这里选(常见的喜阴植物有哪些)
- 这8种耐阴植物,营造阴生植物花境,也是一个不错的选择(营造阴生植物花境)
- 览邦G08 Plus SMART WATCH 测评⑱ 全独立这才是智能手表该有的样子(览邦G08PlusSMART)
- 荣耀手表 GS 3 真机亮相 不支持无线充电(荣耀手表GS3)
热门推荐
- 上不了网怎么知道错误代码(访问网站出现508状态码的解决方法)
- laravel授权怎么用(laravel 使用auth编写登录的方法)
- ASP.NET 生成条形码
- 阿里云docker 基础镜像(Docker 配置阿里云容器服务操作)
- dedecms系统参数设置(织梦dedecms首页调用指定栏目内容的标签详解)
- vue源码系列教程(vue使用引用库中的方法附源码)
- nodejs怎么查看对象的全部属性(浅谈nodejs中创建cluster)
- event.preventDefault方法的使用
- 微信公众号怎么开发页面(微信公众平台开发教程②微信端分享功能图文详解)
- vue实现列表向上滚动更新(vue实现列表无缝滚动)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9