Last active
August 13, 2017 03:13
-
-
Save schedutron/f88c83bf1432ee15e1973624d779f35a to your computer and use it in GitHub Desktop.
A demo script for threading.Condition synchromization primitive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, time | |
from threading import Condition, Thread | |
# This will be used to represent the availability of a produced item. | |
condition = Condition() | |
box = [] | |
def producer(box, nitems): | |
for i in range(nitems): | |
time.sleep(random.randrange(2, 5)) # Sleeps for some time. | |
condition.acquire() | |
num = random.randint(1, 10) | |
box.append(num) # Puts an item into box for consumption. | |
condition.notify() # Notifies the consumer about the availability. | |
print(“Produced:”, num) | |
condition.release() | |
def consumer(box, nitems): | |
for i in range(nitems): | |
condition.acquire() | |
condition.wait() # Blocks until an item is available for consumption. | |
print(“%s: Acquired: %s” % (time.ctime(), box.pop())) | |
condition.release() | |
threads = [] | |
nloops = random.randrange(3, 6) # Number of times an item will be produced and consumed. | |
for func in [producer, consumer]: | |
threads.append(Thread(target=func, args=(box, nloops))) | |
threads[-1].start() # Starts the thread. | |
for thread in threads: | |
‘’’ | |
Waits for the threads to complete before moving on | |
with the main script. | |
‘’’ | |
thread.join() | |
print(“All done.”) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment