first commit
This commit is contained in:
35
testy/archive/threadcondition.py
Normal file
35
testy/archive/threadcondition.py
Normal file
@ -0,0 +1,35 @@
|
||||
# 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()
|
||||
Reference in New Issue
Block a user