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 ...
随机推荐
- python3 爬虫---爬取豆瓣电影TOP250
第一次爬取的网站就是豆瓣电影 Top 250,网址是:https://movie.douban.com/top250?start=0&filter= 分析网址'?'符号后的参数,第一个参数's ...
- 使用milang出错:LookupError: unknown encoding: idna
今天同事安装了milang,结果发现例如以下出错: Traceback (most recent call last): File "F:\vmid.py", line 11, i ...
- Git版本号控制 为什么那么复杂 头大 (忍不住强烈吐槽)
想把自己的源代码保存到云端.想到了用Github.com,然后便開始看怎么使用GIT. 一開始,没有接触之前,想的非常easy的.应该就跟SVN几乎相同吧.写好了提交就能够了. 只是使用了之后才发现根 ...
- 自学Zabbix3.6.4-触发器triggers dependencies依赖关系
有时,一个主机的可用性取决于另一个主机.如果路由器坏了,某个路由器后面的服务器就会变得不可访问.对于两个主机都配置了触发器,您可能会收到两个主机的通知,而只有路由器是有罪的一方.这是主机之间的一些依赖 ...
- 基于.NET的弹性及瞬间错误处理库Polly
本文基本是官方说明的翻译和总结(https://github.com/App-vNext/Polly) 什么是Polly? Polly是一款基于.NET的弹性及瞬间错误处理库, 它允许开发人员以顺畅及 ...
- 【java提高】---数组增删 list删除 map删除
数组增删 集合删除 1.数组增删 package com.test; import java.util.List; import java.util.ArrayList; import java.ut ...
- 实现我博客旁边的线条效果 html canvas-nest.js 源码
canvas-nest.js 这个js文件可以用来实现炫酷的线条与鼠标进行交互的功能,具体效果如图所示 js具体源码如下: /** * Copyright (c) 2016 hustcc * Lice ...
- 字符截取:cut,格式化输出:printf,字符截取:awk,文件或命令输出编辑:sed
cut 选项 文件名 -f 列号 提取第几列 -d 分隔符 指定分隔符把行分成多列 不能以空格为分隔符. [root@localhost ~]# cat testfile no. name sex s ...
- Java定时器应用
在Java多线程中,有的时候,我们需要按照指定间隔时间来执行一些任务,这时,我们就要用到定时器.我们在这里以Java中的Timer定时器为例,演示定时器的应用. 请看下述代码: import java ...
- bzoj 1570: [JSOI2008]Blue Mary的旅行
Description 在一段时间之后,网络公司终于有了一定的知名度,也开始收到一些订单,其中最大的一宗来自B市.Blue Mary决定亲自去签下这份订单.为了节省旅行经费,他的某个金融顾问建议只购买 ...