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解析xml成对应的html示例分享
Apr 02 Python
利用Psyco提升Python运行速度
Dec 24 Python
实例说明Python中比较运算符的使用
May 13 Python
Python构造自定义方法来美化字典结构输出的示例
Jun 16 Python
mac系统安装Python3初体验
Jan 02 Python
python3实现zabbix告警推送钉钉的示例
Feb 20 Python
python 标准差计算的实现(std)
Jul 29 Python
如何理解python面向对象编程
Jun 01 Python
Python趣味入门教程之循环语句while
Aug 26 Python
提高python代码运行效率的一些建议
Sep 29 Python
全面介绍python中很常用的单元测试框架unitest
Dec 14 Python
python实现层次聚类的方法
Nov 01 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
如何隐藏你的.php文件
2007/01/04 PHP
简单的PHP留言本实例代码
2010/05/09 PHP
PHP sprintf()函数用例解析
2011/05/18 PHP
使用php+apc实现上传进度条且在IE7下不显示的问题解决方法
2013/04/25 PHP
php实现可运算的验证码
2015/11/10 PHP
PHP实现的简单对称加密与解密方法实例小结
2017/08/28 PHP
laravel5.0在linux下解决.htaccess无效和去除index.php的问题
2019/10/16 PHP
《JavaScript高级程序设计》阅读笔记(一) ECMAScript基础
2012/02/27 Javascript
extjs render 用法介绍
2013/09/11 Javascript
javascript模拟php函数in_array
2015/04/27 Javascript
Angularjs---项目搭建图文教程
2016/07/08 Javascript
Bootstrap导航条可点击和鼠标悬停显示下拉菜单
2016/11/25 Javascript
bootstrap fileinput组件整合Springmvc上传图片到本地磁盘
2017/05/11 Javascript
JS实现电商放大镜效果
2017/08/24 Javascript
详解vue通过NGINX部署在子目录或者二级目录实践
2018/09/03 Javascript
JS二级菜单不同实现方法分析【4种方法】
2018/12/21 Javascript
vue-cli 目录结构详细讲解总结
2019/01/15 Javascript
VUE 自定义组件模板的方法详解
2019/08/30 Javascript
详解vue中使用axios对同一个接口连续请求导致返回数据混乱的问题
2019/11/06 Javascript
Python中常用操作字符串的函数与方法总结
2016/02/04 Python
django 按时间范围查询数据库实例代码
2018/02/11 Python
python书籍信息爬虫实例
2018/03/19 Python
python实现五子棋人机对战游戏
2020/03/25 Python
解决Python图形界面中设置尺寸的问题
2020/03/05 Python
解决Keras的自定义lambda层去reshape张量时model保存出错问题
2020/07/01 Python
美国领先的礼品卡网站:GiftCards.com
2016/11/02 全球购物
Kathmandu英国网站:新西兰户外运动品牌
2017/03/27 全球购物
Engel & Bengel官网:婴儿推车、儿童房家具和婴儿设备
2019/12/28 全球购物
大学生毕业自荐信
2013/10/10 职场文书
遵纪守法演讲稿
2014/05/23 职场文书
法定代表人免职证明
2015/06/24 职场文书
党风廉洁教育心得体会
2016/01/20 职场文书
创业计划书之花店
2019/09/20 职场文书
left join、inner join、right join的区别
2021/04/05 MySQL
SQLServer RANK() 排名函数的使用
2022/03/23 SQL Server
vue 自定义组件添加原生事件
2022/04/21 Vue.js