Created
June 10, 2014 17:58
-
-
Save mfm24/7b58ecf1e58424365b12 to your computer and use it in GitHub Desktop.
This file contains 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
# 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