python中threading模块详解(一)
来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html
threading提供了一个比thread模块更高层的API来提供线程的并发性。这些线程并发运行并共享内存。
下面来看threading模块的具体用法:
一、Thread的使用 目标函数可以实例化一个Thread对象,每个Thread对象代表着一个线程,可以通过start()方法,开始运行。
这里对使用多线程并发,和不适用多线程并发做了一个比较:
首先是不使用多线程的操作:
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/python#compare for multi threadsimport timedef worker(): print "worker" time.sleep(1) returnif __name__ == "__main__": for i in xrange(5): worker() |
执行结果如下:

下面是使用多线程并发的操作:
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#!/usr/bin/pythonimport threadingimport timedef worker(): print "worker" time.sleep(1) returnfor i in xrange(5): t = threading.Thread(target=worker) t.start() |

可以明显看出使用了多线程并发的操作,花费时间要短的很多。
二、threading.activeCount()的使用,此方法返回当前进程中线程的个数。返回的个数中包含主线程。
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/usr/bin/python#current's number of threadsimport threadingimport timedef worker(): print "test" time.sleep(1)for i in xrange(5): t = threading.Thread(target=worker) t.start()print "current has %d threads" % (threading.activeCount() - 1) |

三、threading.enumerate()的使用。此方法返回当前运行中的Thread对象列表。
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/usr/bin/python#test the variable threading.enumerate()import threadingimport timedef worker(): print "test" time.sleep(2)threads = []for i in xrange(5): t = threading.Thread(target=worker) threads.append(t) t.start()for item in threading.enumerate(): print itemprintfor item in threads: print item |

四、threading.setDaemon()的使用。设置后台进程。
代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#!/usr/bin/python#create a daemonimport threadingimport timedef worker(): time.sleep(3) print "worker"t=threading.Thread(target=worker)t.setDaemon(True)t.start()print "haha" |

可以看出worker()方法中的打印操作并没有显示出来,说明已经成为后台进程。
python中threading模块详解(一)的更多相关文章
- Python中time模块详解
Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...
- python中socket模块详解
socket模块简介 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket通常被叫做"套接字",用于描述IP地址和端口,是一个通信 ...
- python中常用模块详解二
log模块的讲解 Python 使用logging模块记录日志涉及四个主要类,使用官方文档中的概括最为合适: logger提供了应用程序可以直接使用的接口API: handler将(logger创建的 ...
- Python中time模块详解(转)
在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. 在开始之前,首先要说明这几点: ...
- python中常用模块详解一
1.time 模块 import time s = time.localtime() # 把时间转化成格式化的时间,通过. 取得里面的年月日等 struct_time 格式 time.struct_t ...
- Python中pymysql模块详解
安装 pip install pymysql 使用操作 执行SQL #!/usr/bin/env pytho # -*- coding:utf-8 -*- import pymysql # 创建连接 ...
- (转)python标准库中socket模块详解
python标准库中socket模块详解 socket模块简介 原文:http://www.lybbn.cn/data/datas.php?yw=71 网络上的两个程序通过一个双向的通信连接实现数据的 ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- python之sys模块详解
python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...
随机推荐
- div仿textarea
CSS代码: .test_box { width: 400px; min-height: 120px; max-height: 300px; _height: 120px; margin-left: ...
- 【小月博客】用HTML5的File API做上传图片预览功能
前段时间做了一个项目,涉及到上传本地图片以及预览的功能,正好之前了解过 html5(点击查看更多关于web前端的有关资源) 可以上传本地图片,然后再网上看了一些demo结合自己的需求,终于搞定了.(P ...
- Windows无法启动MySQL服务,错误 1053
解决方法: 正常重启电脑试试看,如果不行,在用下面试试 MYSQL 1053错误 解决方法: 在DOS命令行使用 第一步: 运行 -> cmd + 回车 (输入下面的命令) mysqld-nt ...
- pwnable.kr-collision
题目: 链接后登陆 ssh col@pwnable.kr -p2222 查看文件以及权限 Ls –al 查看代码 cat col.c 根据 if(strlen(argv[1]) != 20){ pri ...
- bzoj 3122: [Sdoi2013]随机数生成器
#include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...
- CodeForces 360E Levko and Game(Codeforces Round #210 (Div. 1))
题意:有一些无向边m条权值是给定的k条权值在[l,r]区间可以由你来定,一个点s1 出发一个从s2出发 问s1 出发的能不能先打到f 思路:最短路. 首先检测能不能赢 在更新的时候 如果对于一条边 ...
- EMS问题
如果EMS启动后在运行时报出 JMS error: "Not allowed to create destination这个错误,可能就是你启动方式的问题了 进入到EMS的安装目录的bin目 ...
- 实现手机扫描二维码页面登录,类似web微信-第三篇,手机客户端
转自:http://www.cnblogs.com/fengyun99/p/3541254.html 上一篇,介绍了二维码生成的机制,紧接着,我们就要开发手机客户端来识别这个二维码. 二维码,实际上是 ...
- bzero函数
函数原型:void bzero(void *s,int n) 作用:bzero函数的作用是将s指针指向的地址的前n个字节清零. 头文件:#include <string.h> eg.
- SQLite简单使用说明
System.Data.SQLite.dll下载地址 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 选择. ...