python thread 并发且顺序运行示例


Posted in Python onApril 09, 2009
#-*- coding:utf-8 -*- 
import threading 
import time 
def fun(name, ls_name, front_thread = None): 
''''' 
线程启动函数 
通过front_thread来使用线程有序的运行 
''' 
time.clock() 
time.sleep(2) 
# 如果front_thread存在,则在front_thread运行完成后,才运行当前线程 
if front_thread != None: 
front_thread.join() 
ls_name.append(name) 
print "thread %s : %s"% (name, time.clock()) if __name__ == '__main__': 
ls_result_name = [] 
ls_thread = [] 
time.clock() 
# 逐一启动1000个线程 
for i in range(0,10): 
if len(ls_thread) == 0: 
t = threading.Thread(target=fun, args=(i,ls_result_name,None)) 
else: 
t = threading.Thread(target=fun, args=(i,ls_result_name,ls_thread[-1])) 
t.start() 
ls_thread.append(t) 
# 等待所有线程结束 
for t in ls_thread: 
t.join() 
print 'ls_result_name:', ls_result_name 
print "main thread:%s" % time.clock()

运行结果为:
thread 0 : 1.99962006344
thread 1 : 2.00000866032
thread 2 : 2.00059113658
thread 3 : 2.00080345407
thread 4 : 2.00100068584
thread 5 : 2.00119456523
thread 6 : 2.00138593033
thread 7 : 2.00166753037
thread 8 : 2.00211758757
thread 9 : 2.0024776892
ls_result_name: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
main thread:2.003211302
线程更明细的使用可参考:
http://docs.python.org/library/threading.html
time.clock模块的更详细介绍可参考:
http://blog.csdn.net/kiki113/archive/2009/03/28/4033017.aspx
Python 相关文章推荐
实例讲解Python中函数的调用与定义
Mar 14 Python
浅谈python函数之作用域(python3.5)
Oct 27 Python
python 实现数组list 添加、修改、删除的方法
Apr 04 Python
Python实现监控Nginx配置文件的不同并发送邮件报警功能示例
Feb 26 Python
pyqt 多窗口之间的相互调用方法
Jun 19 Python
python实现电子书翻页小程序
Jul 23 Python
Django 实现admin后台显示图片缩略图的例子
Jul 28 Python
pandas 对日期类型数据的处理方法详解
Aug 08 Python
python 中xpath爬虫实例详解
Aug 26 Python
Python实现串口通信(pyserial)过程解析
Sep 25 Python
Django2 连接MySQL及model测试实例分析
Dec 10 Python
Python基础之赋值,浅拷贝,深拷贝的区别
Apr 30 Python
python 判断一个进程是否存在
Apr 09 #Python
python ElementTree 基本读操作示例
Apr 09 #Python
python 获取et和excel的版本号
Apr 09 #Python
python启动办公软件进程(word、excel、ppt、以及wps的et、wps、wpp)
Apr 09 #Python
python 获取文件列表(或是目录例表)
Mar 25 #Python
Python字符串的encode与decode研究心得乱码问题解决方法
Mar 23 #Python
python 判断自定义对象类型
Mar 21 #Python
You might like
Apache环境下PHP利用HTTP缓存协议原理解析及应用分析
2010/02/16 PHP
PHP6 中可能会出现的新特性预览
2014/04/04 PHP
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
2014/06/12 PHP
php简单判断两个字符串是否相等的方法
2015/07/13 PHP
yii2 modal弹窗之ActiveForm ajax表单异步验证
2016/06/13 PHP
php微信公众平台开发(一) 配置接口
2016/12/06 PHP
PHP编程快速实现数组去重的方法详解
2017/07/22 PHP
导入extjs、jquery 文件时$使用冲突问题解决方法
2014/01/14 Javascript
使用Javascript监控前端相关数据的代码
2016/10/27 Javascript
JS图片轮播与索引变色功能实例详解
2017/07/06 Javascript
es6在react中的应用代码解析
2017/11/08 Javascript
jQuery实现条件搜索查询、实时取值及升降序排序的方法分析
2019/05/04 jQuery
微信小程序系列之自定义顶部导航功能
2019/05/21 Javascript
深入理解python多进程编程
2016/06/12 Python
python实现拓扑排序的基本教程
2018/03/11 Python
Python利用公共键如何对字典列表进行排序详解
2018/05/19 Python
浅谈Python 列表字典赋值的陷阱
2019/01/20 Python
python  文件的基本操作 菜中菜功能的实例代码
2019/07/17 Python
python爬取百度贴吧前1000页内容(requests库面向对象思想实现)
2019/08/10 Python
Django视图扩展类知识点详解
2019/10/25 Python
python数据预处理 :样本分布不均的解决(过采样和欠采样)
2020/02/29 Python
html5唤起app的方法
2017/11/30 HTML / CSS
Magee 1866官网:Donegal粗花呢外套和大衣专家
2019/11/01 全球购物
管道维修工岗位职责
2013/12/27 职场文书
公司会计主管岗位责任制
2014/03/01 职场文书
银行爱岗敬业演讲稿
2014/05/05 职场文书
幼儿园六一儿童节活动方案
2014/08/26 职场文书
党员个人对照检查材料思想汇报
2014/09/16 职场文书
推普周国旗下讲话稿
2014/09/21 职场文书
一份教室追逐打闹的检讨书
2014/09/27 职场文书
受资助学生感谢信
2015/01/21 职场文书
教师节主题班会教案
2015/08/17 职场文书
小学五年级(说明文3篇)
2019/08/13 职场文书
竞选稿之小学班干部
2019/10/31 职场文书
PHP控制循环操作的时间
2021/04/01 PHP
一文搞懂python异常处理、模块与包
2021/06/26 Python