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 调钉钉接口(python3实现钉钉消息推送的方法示例)
- python微信红包分配(PHP切割整数工具类似微信红包金额分配的思路详解)
- python中for循环计算1-100(python 用for循环实现1~n求和的实例)
- python模块光照强度(我如何用 CircuitPython 和开源工具监控温室)
- python与php(解决Python3 被PHP程序调用执行返回乱码的问题)
- python微信自动化(python微信撤回监测代码)
- 如何使用python定时运行(Python实现定时执行任务的三种方式简单示例)
- python指定参数教程(详解Python传入参数的几种方法)
- python爬网验证码在哪里(详解python 爬取12306验证码)
- pythonpandas数据类型(详解Python数据分析--Pandas知识点)
- python批量创建字典(Python编写合并字典并实现敏感目录的小脚本)
- python使用telnet(python 处理telnet返回的More,以及get想要的那个参数方法)
- pythondjango后台管理(基于腾讯云服务器部署微信小程序后台服务Python+Django)
- 百度免费翻译接口(基于python实现百度翻译功能)
- python进程管理教程(Python I/O与进程的详细讲解)
- 今日菜价 芥兰涨幅最高 1.33 ,花菜降幅最高 3.10(今日菜价芥兰涨幅最高)
- 今日菜价 椰菜涨幅最高 3.25 ,水空心菜降幅最高 2.58(今日菜价椰菜涨幅最高)
- 今日菜价 红三鱼涨幅最高 4.41 ,黄鳝降幅最高 5.06(红三鱼涨幅最高)
- 今日菜价 西生菜涨幅最高 6.19 ,生菜降幅最高 5.38(西生菜涨幅最高)
- 今日菜价 青豆角涨幅最高 0.70 ,菜心降幅最高 5.55(青豆角涨幅最高)
- 农村植物,龙芽草若长在您家路旁,请珍惜,它对抗癌有特效(龙芽草若长在您家路旁)
热门推荐
- mysql 5.5.27 winx64安装配置方法图文教程(mysql 5.5.27 winx64安装配置方法图文教程)
- dedecms开发教程(织梦DedeCms调用全站相关文章方法)
- ASP.NET实现多文件上传
- 云服务器怎么上传16g文件(云服务器怎么上传下载文件?)
- javascript写游戏脚本(原生JS实现飞机大战小游戏)
- css图片颜色提取(解析CSS 提取图片主题色功能小技巧)
- yii2和laravel框架哪个比较简单(关于Yii中模型场景的一些简单介绍)
- centos7上安装python(centos6.5安装python3.7.1之后无法使用pip的解决方案)
- css3动画效果怎么调(CSS3动画之流彩文字效果+图片模糊效果+边框伸展效果实现代码合集)
- sql重复记录怎么删除(SQL去除重复记录七种)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9