python threading模块
来源:互联网 发布:sql获取字符串长度函数 编辑:程序博客网 时间:2024/06/06 07:20
python threading模块
首先我们来看看threading包含那些属性和方法吧(官方文档在http://docs.python.org/library/threading.html)
就我一直不太明白的几个点开始吧
1. Condition
A factory function that returns a new condition variable object. A condition variable allows one or more threads to wait until they are notified by another thread.
Condition涉及到的几个方法acquire, release,wait,notify,
2 class Boy(threading.Thread):
3 def __init__(self, cond, name):
4 super(Boy, self).__init__()
5 self.cond= cond
6 self.name= name
7
8 def run(self):
9 self.cond.acquire()
10 print self.name+': 嫁给我吧!?'
11 self.cond.notify()#唤醒一个挂起的线程,让hanmeimei表态
12 self.cond.wait()#释放内部所占用的琐,同时线程被挂起,直至接收到通知被唤醒或超时,等待hanmeimei回答
13
14 print self.name+': 我单下跪,送上戒指!'
15 self.cond.notify()
16 self.cond.wait()
17
18 print self.name+': Li太太,你的选择太明治了。'
19 self.cond.release()
20
21 class Girl(threading.Thread):
22 def__init__(self, cond, name):
23 super(Girl, self).__init__()
24 self.cond = cond
25 self.name = name
26 def run(self):
27 self.cond.acquire()
28 self.cond.wait()#等待Lilei求婚
29 print self.name+': 没有情调,不够浪漫,不答应'
30 self.cond.notify()
31 self.cond.wait()
32
33
34 print self.name +': 好吧,答应你了'
35 self.cond.notify()
36 self.cond.release()
37
38 cond = threading.Condition()
39 boy = Boy(cond, 'LiLei')
40 girl = Girl(cond, 'HanMeiMei')
41 girl.start()
42 boy.start()
运行结果是
LiLei: 嫁给我吧!?
HanMeiMei: 没有情调,不够浪漫,不答应
LiLei: 我单下跪,送上戒指!
HanMeiMei: 好吧,答应你了
LiLei: Li太太,你的选择太明治了。
2. Event
A factory function that returns a new event object. An event manages a flag that can be set to true with theset() method and reset to false with theclear() method. Thewait() method blocks until the flag is true.
个人任务Event是个简易的Condition,还是用上面求婚做哥例子吧
import
threading, time
class
Boy(threading.Thread):
def
__init__(
self
, cond, name):
super
(Boy,
self
).__init__()
self
.cond
=
cond
self
.name
=
name
def
run(
self
):
print
self
.name
+
': 嫁给我吧!?'
self
.cond.
set
()
#唤醒一个挂起的线程,让hanmeimei表态
time.sleep(
0.5
)
self
.cond.wait()
print
self
.name
+
': 我单下跪,送上戒指!'
self
.cond.
set
()
time.sleep(
0.5
)
self
.cond.wait()
self
.cond.clear()
print
self
.name
+
': Li太太,你的选择太明治了。'
class
Girl(threading.Thread):
def
__init__(
self
, cond, name):
super
(Girl,
self
).__init__()
self
.cond
=
cond
self
.name
=
name
def
run(
self
):
self
.cond.wait()
#等待Lilei求婚
self
.cond.clear()
print
self
.name
+
': 没有情调,不够浪漫,不答应'
self
.cond.
set
()
time.sleep(
0.5
)
self
.cond.wait()
print
self
.name
+
': 好吧,答应你了'
self
.cond.
set
()
cond
=
threading.Event()
boy
=
Boy(cond,
'LiLei'
)
girl
=
Girl(cond,
'HanMeiMei'
)
boy.start()
girl.start()
3. Lock
A primitive lock is a synchronization primitive that is not owned by a particular thread when locked. In Python, it is currently the lowest level synchronization primitive available, implemented directly by thethread extension module.
4. RLock
A reentrant lock is a synchronization primitive that may be acquired multiple times by the same thread. Internally, it uses the concepts of “owning thread” and “recursion level” in addition to the locked/unlocked state used by primitive locks. In the locked state, some thread owns the lock; in the unlocked state, no thread owns it.
这两种琐的主要区别是:RLock允许在同一线程中被多次acquire。而Lock却不允许这种情况。注意:如果使用RLock,那么acquire和release必须成对出现,即调用了n次acquire,必须调用n次的release才能真正释放所占用的琐。
5. Thread
上面的例子也很清楚,
join
主调线程堵塞,直到被调用线程运行结束或超时.
daemon(文档上这么写的,不理解)
布尔值。True表示为守护线程。该线程的设置必须在start()之前。初始值从你线程继承而来,主线程为非daemon线程。
对于共享数据的同步问题,我们选两个比较常用的方法来解决。
6. Timer
比较简单直观
2 print"hello, world"
3 t = Timer(3, hello)
4 t.start()
剩下的一些threading的方法比较好理解currentThread,activeCount,enumerate
- Python threading模块
- Python threading模块简介
- python threading模块
- Python 多线程 Threading模块
- Python的threading模块
- PYTHON THREADING模块
- python中的threading模块
- python threading多线程模块
- python多线程-threading模块
- Python标准模块--threading
- Python threading模块初探
- Python标准模块--threading
- Python Threading 模块
- python多线程模块threading
- python的threading模块中threading.RLock和threading.Lock
- python多线程模块threading简例
- [Python]多线程--threading模块实现
- Python 多线程(threading模块)
- iso文件制作
- IE旧版本如何让HTML4转换并且支持HTML5的新特性。
- 网址链接的问题,少了http就链接不上
- 详解linux vi命令用法
- VC++实现HTTP代理
- python threading模块
- 我们在囧途之软件投标篇
- 如何定制Windows Embedded Compact 7 shell详解(下)
- Ubuntu-如何隐藏文件/文件夹
- oracle 11g em登录界面乱码
- MyBatis 入门到精通(二) SQL语句映射XML文件 .
- JAVA程序员应该知道的linux命令
- Bloom Filter
- ubuntu系统目录结构