Python:使用threading模块实现多线程编程七[使用Condition实现复杂同步]

目前我们已经会使用Lock去对公共资源进行互斥访问了,也探讨了同一线程可以使用RLock去重入锁,但是尽管如此我们只不过才处理了一些程序中简单的同步现象,我们甚至还不能很合理的去解决使用Lock锁带来的死锁问题。所以我们得学会使用更深层的解决同步问题。

        Python提供的Condition对象提供了对复杂线程同步问题的支持。Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还提供了wait和notify方法。

        使用Condition的主要方式为:线程首先acquire一个条件变量,然后判断一些条件。如果条件不满足则wait;如果条件满足,进行一些处理改变条件后,通过notify方法通知其他线程,其他处于wait状态的线程接到通知后会重新判断条件。不断的重复这一过程,从而解决复杂的同步问题。

        下面我们通过很著名的“生产者-消费者”模型来来演示下,在Python中使用Condition实现复杂同步。

Python代码
  1. ''''' 
  2. Created on 2012-9-8 
  3.   
  4. @author: walfred 
  5. @module: thread.TreadTest7 
  6. '''  
  7.   
  8. import threading  
  9. import time  
  10.    
  11. condition = threading.Condition()  
  12. products = 0  
  13.    
  14. class Producer(threading.Thread):  
  15.     def __init__(self):  
  16.         threading.Thread.__init__(self)  
  17.           
  18.     def run(self):  
  19.         global condition, products  
  20.         while True:  
  21.             if condition.acquire():  
  22.                 if products < 10:  
  23.                     products += 1;  
  24.                     print "Producer(%s):deliver one, now products:%s" %(self.name, products)  
  25.                     condition.notify()  
  26.                 else:  
  27.                     print "Producer(%s):already 10, stop deliver, now products:%s" %(self.name, products)  
  28.                     condition.wait();  
  29.                 condition.release()  
  30.                 time.sleep(2)  
  31.           
  32. class Consumer(threading.Thread):  
  33.     def __init__(self):  
  34.         threading.Thread.__init__(self)  
  35.           
  36.     def run(self):  
  37.         global condition, products  
  38.         while True:  
  39.             if condition.acquire():  
  40.                 if products > 1:  
  41.                     products -= 1  
  42.                     print "Consumer(%s):consume one, now products:%s" %(self.name, products)  
  43.                     condition.notify()  
  44.                 else:  
  45.                     print "Consumer(%s):only 1, stop consume, products:%s" %(self.name, products)  
  46.                     condition.wait();  
  47.                 condition.release()  
  48.                 time.sleep(2)  
  49.                   
  50. if __name__ == "__main__":  
  51.     for p in range(02):  
  52.         p = Producer()  
  53.         p.start()  
  54.           
  55.     for c in range(010):  
  56.         c = Consumer()  
  57.         c.start()  

        代码中主要实现了生产者和消费者线程,双方将会围绕products来产生同步问题,首先是2个生成者生产products ,而接下来的10个消费者将会消耗products,代码运行如下:

Producer(Thread-1):deliver one, now products:1
Producer(Thread-2):deliver one, now products:2
Consumer(Thread-3):consume one, now products:1
Consumer(Thread-4):only 1, stop consume, products:1
Consumer(Thread-5):only 1, stop consume, products:1
Consumer(Thread-6):only 1, stop consume, products:1
Consumer(Thread-7):only 1, stop consume, products:1
Consumer(Thread-8):only 1, stop consume, products:1
Consumer(Thread-10):only 1, stop consume, products:1
Consumer(Thread-9):only 1, stop consume, products:1
Consumer(Thread-12):only 1, stop consume, products:1
Consumer(Thread-11):only 1, stop consume, products:1

        另外:Condition对象的构造函数可以接受一个Lock/RLock对象作为参数,如果没有指定,则Condition对象会在内部自行创建一个RLock;除了notify方法外,Condition对象还提供了notifyAll方法,可以通知waiting池中的所有线程尝试acquire内部锁。由于上述机制,处于waiting状态的线程只能通过notify方法唤醒,所以notifyAll的作用在于防止有线程永远处于沉默状态。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值