python threading模块

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,

复制代码
1 import threading, time
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 the set() method and reset to false with the clear() method. The wait() method blocks until the flag is true.

个人任务Event是个简易的Condition,还是用上面求婚做哥例子吧

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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 the thread 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

比较简单直观

1 def hello():
2     print " hello, world "
3     t = Timer( 3 , hello)
4     t.start()

剩下的一些threading的方法比较好理解currentThread,activeCount,enumerate

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值