Python模块学习------ 多线程threading(1)
# Method 1: 创建一个Thread实例,传给它一个函数; import threading
from time import sleep, ctime loops = [4,2] def loop(nloop, nsec):
print "start loop", nloop, "at:",ctime()
sleep(nsec)
print "end loop", nloop, "at:",ctime() def main():
print "*****start*********", ctime()
threads = []
nloops = range(len(loops)) for i in nloops:
t = threading.Thread(target=loop, args=(i, loops[i]))
threads.append(t) # start threads
for i in nloops:
threads[i].start() # wait for all threads to finish
for i in nloops:
threads[i].join() print "*******end*******", ctime() if __name__ == "__main__":
main()
# Method 2: 创建一个Thread的实例,传给它一个可调用的类对象
import threading
from time import sleep, ctime loops = [4,2] class ThreadFunc(object): def __init__(self, func, args, name =''):
self.name = name
self.func = func
self.args = args def __call__(self):
apply(self.func, self.args) def loop(nloop, nsec):
print "start loop", nloop, "at:",ctime()
sleep(nsec)
print "end loop", nloop, "at:",ctime() def main():
print "*****start*********", ctime()
threads = []
nloops = range(len(loops)) # create all threads
for i in nloops:
t = threading.Thread(target=ThreadFunc(loop, (i,loops[i]), loop.__name__))
threads.append(t) # start all threads
for i in nloops:
threads[i].start() # wait for all threads to finish
for i in nloops:
threads[i].join() # join() 程序挂起,直至线程结束 print "*******end*******", ctime() if __name__ == "__main__":
main()
Python模块学习------ 多线程threading(1)的更多相关文章
- Python模块学习------ 多线程threading(2)
一.避免使用thread模块,使用threading模块的原因: 1. 更高级别的threading模块更为先进,对线程的支持更加完善.而且使用thread模块的属性有可能会与threading 出现 ...
- Python模块学习:threading 多线程控制和处理
Reference:http://python.jobbole.com/81546/ threading.Thread Thread 是threading模块中最重要的类之一,可以使用它来创建线程.有 ...
- 【转】Python模块学习 - fnmatch & glob
[转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...
- 【目录】Python模块学习系列
目录:Python模块学习笔记 1.Python模块学习 - Paramiko - 主机管理 2.Python模块学习 - Fileinput - 读取文件 3.Python模块学习 - Confi ...
- Python模块学习filecmp文件比较
Python模块学习filecmp文件比较 filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单.python标准库还提供了difflib模块用于比较文件的内容.关于dif ...
- python模块学习第 0000 题
将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果. 类似于图中效果: 好可爱>%<! 题目来源:https://github.com/Yixiao ...
- Python模块学习:logging 日志记录
原文出处: DarkBull 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net ...
- 解惑Python模块学习,该如何着手操作...
Python模块 晚上和朋友聊天,说到公司要求精兵计划,全员都要有编程能力.然后C.Java.Python-对于零基础入门的,当然是选择Python的人较多了.可朋友说他只是看了简单的语法,可pyth ...
- Python模块学习
6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...
随机推荐
- 记一次高并发场景下.net监控程序数据上报的性能调优
最近在和小伙伴们做充电与通信程序的架构迁移.迁移前的架构是,通信程序负责接收来自充电集控设备的数据实时数据,通过Thrift调用后端的充电服务,充电服务收到响应后放到进程的Queue中,然后在管理线程 ...
- 一道题Wrong Answer之后该何去何从?
写程序手不稳是个大毛病,往往会让一份能AC的代码变成99.995%正确,失之毫厘谬以千里,近期十场个人赛非常少有能一次AC的经历,细致想想除了根本逻辑上的错误.大概都是跪在这些细节上: 1.输出格式, ...
- Linux性能及调优指南(翻译)之Linux进程管理
本文为IBM RedBook的Linux Performanceand Tuning Guidelines的1.1节的翻译原文地址:http://www.redbooks.ibm.com/redpap ...
- xcode 6 改动组织及开发人员
搞个ios 开发死去活来的,各个地方说的都不一样,defaults write 不好用 在xcode6中 改动方法例如以下 改动create by __FULLUSERNAME___ 部分 Syste ...
- EventBus猜想 ----手把手带你自己实现一个EventBus
本文是什么 本文是一篇怀着推測角度学习一个未知东西(EventBus)的文章. 先推測EventBus是怎样实现的. 依据推測去模仿他的实现. 查看源代码.验证猜想. 更深入的去理解他. 转载请注明出 ...
- redis的set类型
1.简单描述 redis的set类型是string类型数值的无序集合.set元素最大可以包含2的32次方减1个元素.由于set类型是通过hash table实现(旧版本是这样,新版本不确定是不是改用了 ...
- git 设置和取消代理
# 设置ss git config --global http.proxy 'socks5://127.0.0.1:1080' git config --global https.proxy 'soc ...
- AI 系列 总目录
AI 系列 答应了园区大牛 张善友 要写AI 的系列博客,所以开始了AI 系列之旅. 一.四大平台系列(百度AI.阿里ET.腾讯.讯飞) 1.百度篇 (1) 百度OCR文字识别-身份证识别 (2) 基 ...
- Android破解学习之路(四)——Android游戏 3D摩托飞车破解
经过前面三期的破解,想必大家已经非常熟悉破解的流程,这一篇也算是练手项目,我们继续来练习吧 apk下载地址:链接: https://pan.baidu.com/s/1sl3b3R3 密码: 6666 ...
- 大赛获奖选手专访 | 冷燕冰:最佳设计奖TIMING里的时机和时序
Mockplus三周年原型设计大赛,从筹备到11月21日完美落幕,50余天的时光,已成为过去.这场近千人参赛的原型设计大赛,我想,无论是于主办方,于参赛选手,于专家评委,还是于每一个关注和参与的人,都 ...