多线程/多进程同步之Condition(生产者-消费者问题)

标准库模块threading中提供了一个类对象Condition,用于表示带触发条件的锁,以帮助我们处理多线程间复杂的同步问题。例如生产者消费者问题。
Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还提供了wait和notify方法。
Condition允许一个或多个线程等待触发条件,直到收到另外一个线程的通知。使用Condition的主要方式为:线程首先acquire一个条件变量,然后判断一些条件。
如果条件不满足则wait;如果条件满足,进行一些处理改变条件后,通过notify方法通知其他线程,其他处于wait状态的线程接到通知后会重新判断条件。
不断的重复这一过程,从而解决复杂的同步问题。

•threading.Condition([lock]):创建一个condition,支持从外界引用一个Lock对象(适用于多个condtion共用一个Lock的情况),默认是创建一个新的Lock对象。

•acquire()/release():获得/释放 Lock

•wait([timeout]):线程挂起,直到收到一个notify通知或者超时(可选的,浮点数,单位是秒s)才会被唤醒继续运行。wait()必须在已获得Lock前提下才能调用,
否则会触发RuntimeError。调用wait()会释放Lock,直至该线程被Notify()、NotifyAll()或者超时线程又重新获得Lock.

#调用wait后的4个动作
1,该线程被阻塞。
2,该线程被放到条件等待池。
3,释放锁。
4,开始等待;此时锁等待池中的某个线程将获得锁。

•notify(n=1):通知其他线程,那些挂起的线程接到这个通知之后会开始运行,默认是通知一个正等待该condition的线程,
最多则唤醒n个等待的线程。notify()必须在已获得Lock前提下才能调用,否则会触发RuntimeError。notify()不会主动释放Lock。
#调用notify后的两个处理
1,通知条件等待池中的线程。
2,被通知的线程自动调用方法acquire()试图获得锁。
•notifyAll(): 如果wait状态线程比较多,notifyAll的作用就是通知所有线程。

#【1】多线程同步之Condition

 1 from threading import Thread, Condition
 2 import time
 3 
 4 cond = Condition()
 5 
 6 class Thread1(Thread):
 7     def __init__(self, name):
 8         super().__init__(name=name)
 9 
10     def run(self):
11         cond.acquire()
12 
13         print('%s说:1' % self.name)
14         cond.notify()    #
15         cond.wait()
16         #等待两秒
17         time.sleep(2)
18         print('%s说:11' % self.name)
19         cond.notify()
20         cond.wait()
21 
22         time.sleep(2)
23         print('%s说:111' % self.name)
24         cond.notify()
25 
26         cond.release()
27 
28 class Thread2(Thread):
29     def __init__(self, name):
30         super().__init__(name=name)
31 
32     def run(self):
33         time.sleep(1)
34         cond.acquire()
35 
36         time.sleep(2)
37         print('%s说:2' % self.name)
38         cond.notify()
39         cond.wait()
40 
41         time.sleep(2)
42         print('%s说:22' % self.name)
43         cond.notify()
44         cond.wait()
45 
46         time.sleep(2)
47         print('%s说:222' % self.name)
48 
49         cond.release()
50 
51 Thread1('Thread1').start()
52 Thread2('Thread2').start()
View Code

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

#【2】多进程同步之Condition

 1 from multiprocessing import Process, Condition
 2 import time
 3 
 4 cond = Condition()
 5 
 6 class MyProcess1(Process):
 7     def __init__(self, name):
 8         super().__init__(name=name)
 9 
10     def run(self):
11         cond.acquire()
12 
13         print('%s说:1' % self.name)
14         cond.notify()
15         cond.wait()
16 
17         # 等待2秒
18         time.sleep(2)
19         print('%s说:11' % self.name)
20         cond.notify()
21         cond.wait()
22 
23         time.sleep(2)
24         print('%s说:111' % self.name)
25         cond.notify()
26 
27         cond.release()
28 
29 class MyProcess2(Process):
30     def __init__(self, name):
31         super().__init__(name=name)
32 
33     def run(self):
34         time.sleep(1)
35         cond.acquire()
36 
37         # 等待2秒
38         time.sleep(2)
39         print('%s说:2' % self.name)
40         cond.notify()
41         cond.wait()
42 
43         time.sleep(2)
44         print('%s说:22' % self.name)
45         cond.notify()
46         cond.wait()
47 
48         time.sleep(2)
49         print('%s说:222' % self.name)
50 
51         cond.release()
52 
53 MyProcess1('Process1').start()
54 MyProcess2('Process2').start()
View Code

★生产者消费者问题

假设有一群生产者和一群消费者通过市场来交换产品。
生产者:如果市场上剩余的产品小于20个,就生产4个产品放到市场上。
消费者:如果市场上剩余的产品多余10个,那么就从市场上消费3个产品。

#【3】生产者消费者问题-多线程Condition

 1 from threading import Thread, Condition
 2 import time
 3 
 4 cond = Condition()
 5 count = 0
 6 
 7 class Producer(Thread):
 8     def run(self):
 9         global count, cond
10         while True:
11             cond.acquire()
12             if count < 20:
13                 count += 4
14                 print('%s:生产者生产了4个,当前总共%d个' % (self.name, count))
15                 cond.notify()
16             else:
17                 print('%s:不生产,等待' % self.name)
18                 cond.wait()
19             cond.release()
20             time.sleep(2)
21 
22 class Consumer(Thread):
23     def run(self):
24         global count, cond
25         while True:
26             cond.acquire()
27             if count > 10:
28                 count -= 3
29                 print('%s:消费者消费了3个,当前总共%d个' % (self.name, count))
30                 cond.notify()
31             else:
32                 print('%s:不消费,等待' % self.name)
33                 cond.wait()
34             cond.release()
35             time.sleep(2)
36 
37 for i in range(3):
38     Producer().start()
39 
40 for i in range(3):
41     Consumer().start()
View Code

#使用队列

 1 import time,random
 2 import queue,threading
 3 
 4 q = queue.Queue()
 5 
 6 def Producer(name):
 7   count = 0
 8   while count <20:
 9     print("开始生产")
10     time.sleep(random.randrange(3))
11     q.put(count)
12     print('生产者 %s 已生产 %s ..' %(name, count))
13     count +=1
14     #q.task_done()#生产完毕,通知消费的q.join(),q.join()后面的语句才会被执行。
15     #q.join()
16     print("ok......")
17 def Consumer(name):
18   count = 0
19   while count <20:
20     time.sleep(random.randrange(4))
21     if not q.empty():
22         data = q.get()
23         #q.task_done()
24         #q.join() #接收生产者q.task_done的通知,然后执行下面语句,否则,阻塞,等待通知。
25         print('消费者 %s 已消费 %s ..' %(name, data))
26     else:
27         print("-----无剩余了----")
28     count +=1
29 
30 p1 = threading.Thread(target=Producer, args=('生产者1',))
31 c1 = threading.Thread(target=Consumer, args=('消费者1',))
32 c2 = threading.Thread(target=Consumer, args=('消费者2',))
33 c3 = threading.Thread(target=Consumer, args=('消费者3',))
34 p1.start()
35 c1.start()
36 c2.start()
37 c3.start()
View Code

#【4】生产者消费者问题-多进程Condition

 1 from multiprocessing import Process, Value, Condition
 2 import time
 3 
 4 cond = Condition()
 5 count = Value('i', 0)
 6 
 7 class Producer(Process):
 8     def run(self):
 9         global count, cond
10         while True:
11             cond.acquire()
12             if count.value < 20:
13                 count.value += 4
14                 print('%s:生产者生产了4个,当前总共%d个' % (self.name, count.value))
15                 cond.notify()
16             else:
17                 print('%s:不生产,等待' % self.name)
18                 cond.wait()
19             cond.release()
20             time.sleep(2)
21 
22 class Consumer(Process):
23     def run(self):
24         global count, cond
25         while True:
26             cond.acquire()
27             if count.value > 10:
28                 count.value -= 3
29                 print('%s:消费者消费了3个,当前总共%d个' % (self.name, count.value))
30                 cond.notify()
31             else:
32                 print('%s:不消费,等待' % self.name)
33                 cond.wait()
34             cond.release()
35             time.sleep(2)
36 
37 for i in range(3):
38     Producer().start()
39 
40 for i in range(3):
41     Consumer().start()
View Code

 

posted @ 2019-06-29 18:39  山的那一边  阅读(1293)  评论(0)    收藏  举报