semaphore.release() utils.say("acquiring the semaphore...") semaphore.acquire() # Once the call to .acquire() completes, I own the shared resource and # I'm free to read from the memory. s = utils.read_from_memory(memory) # What I read must be the md5 of what I wrote or something's gone wrong. if PY_MAJOR_VERSION > 2: what_i_wrote = what_i_wrote.encode() try: assert(s == hashlib.md5(what_i_wrote).hexdigest()) except AssertionError: flex_utils.raise_error(AssertionError, "Shared memory corruption after %d iterations." % i) # MD5 the reply and write back to Mrs. Conclusion. if PY_MAJOR_VERSION > 2: s = s.encode() what_i_wrote = hashlib.md5(s).hexdigest() utils.write_to_memory(memory, what_i_wrote) # Announce for one last time that the semaphore is free again so that # Mrs. Conclusion can exit. if not params["LIVE_DANGEROUSLY"]: utils.say("Final release of the semaphore followed by a 5 second pause") semaphore.release() time.sleep(5) # ...before beginning to wait until it is free again.
# If the message is what I wrote, put it back on the queue. while s == what_i_sent: # Nothing new; give Mrs. Conclusion another chance to respond. mq.send(s) s, _ = mq.receive() s = s.decode() utils.say("Received %s" % s) # What I read must be the md5 of what I wrote or something's # gone wrong. if PY_MAJOR_VERSION > 2: what_i_sent = what_i_sent.encode() try: assert (s == hashlib.md5(what_i_sent).hexdigest()) except AssertionError: flex_utils.raise_error(AssertionError, "Message corruption after %d iterations." % i) # MD5 the reply and write back to Mrs. Conclusion. s = hashlib.md5(s.encode()).hexdigest() utils.say("Sending %s" % s) mq.send(s) what_i_sent = s utils.say("") utils.say("%d iterations complete" % (i + 1)) utils.say("Destroying the message queue.") mq.remove()
while s == what_i_sent: # Nothing new; give Mrs. Premise another chance to respond. mq.send(s) s, _ = mq.receive() s = s.decode() utils.say("Received %s" % s) if what_i_sent: if PY_MAJOR_VERSION > 2: what_i_sent = what_i_sent.encode() try: assert(s == hashlib.md5(what_i_sent).hexdigest()) except: flex_utils.raise_error(AssertionError, "Message corruption after %d iterations." % i) #else: # When what_i_sent is blank, this is the first message which # I always accept without question. # MD5 the reply and write back to Mrs. Premise. s = hashlib.md5(s.encode()).hexdigest() utils.say("Sending %s" % s) mq.send(s) what_i_sent = s utils.say("") utils.say("%d iterations complete" % (i + 1))