Skip to content

Instantly share code, notes, and snippets.

@mfm24
Created June 10, 2014 17:58
Show Gist options
  • Save mfm24/7b58ecf1e58424365b12 to your computer and use it in GitHub Desktop.
Save mfm24/7b58ecf1e58424365b12 to your computer and use it in GitHub Desktop.
# how exactly do conditions work??
from threading import Thread, Condition, Lock
import logging
logging.basicConfig()
log = logging.root
log.setLevel(logging.DEBUG)
mylock = Lock()
def wait_for_cond(c, msg):
c.acquire()
while True:
c.wait()
# print not safe here
log.warn(msg + "\n")
def notify_all(c):
c.acquire()
c.notify_all()
c.release()
for i in range(5):
c = Condition(mylock)
globals()["c%s" % i] = c
t = Thread(target = lambda: wait_for_cond(c, "Got %s!" % i))
t.daemon = True
t.start()
globals()["t%s" % i] = t
log.debug("Started 5 threads! Using logging for better multi threading support...")
log.debug("Notifying_all c1...")
notify_all(c1)
log.debug("Notifying_all c1...")
notify_all(c1)
log.debug("Notifying_all c2...")
notify_all(c2)
log.debug("Notifying_all c3...")
notify_all(c3)
x = raw_input("Press key to exit")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment