35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
# A condition variable allows one or more threads to wait until they are
|
|
# notified by another thread.
|
|
|
|
import threading
|
|
import time
|
|
import logging
|
|
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
format='(%(threadName)-9s) %(message)s',)
|
|
|
|
def consumer(cv):
|
|
logging.debug('Consumer thread started ...')
|
|
with cv:
|
|
logging.debug('Consumer waiting ...')
|
|
cv.wait()
|
|
logging.debug('Consumer consumed the resource')
|
|
|
|
def producer(cv):
|
|
logging.debug('Producer thread started ...')
|
|
with cv:
|
|
logging.debug('Making resource available')
|
|
logging.debug('Notifying to all consumers')
|
|
cv.notify_all()
|
|
|
|
if __name__ == '__main__':
|
|
condition = threading.Condition()
|
|
cs1 = threading.Thread(name='consumer1', target=consumer, args=(condition,))
|
|
cs2 = threading.Thread(name='consumer2', target=consumer, args=(condition,))
|
|
pd = threading.Thread(name='producer', target=producer, args=(condition,))
|
|
|
|
cs1.start()
|
|
time.sleep(2)
|
|
cs2.start()
|
|
time.sleep(2)
|
|
pd.start() |