def main(): utils.say("Oooo 'ello, I'm Mrs. Conclusion!") params = utils.read_params() semaphore = sysv_ipc.Semaphore(params["KEY"]) memory = sysv_ipc.SharedMemory(params["KEY"]) utils.say("memory attached at %d" % memory.address) what_i_wrote = "" s = "" for i in range(0, params["ITERATIONS"]): utils.say("i = %d" % i) if not params["LIVE_DANGEROUSLY"]: # Wait for Mrs. Premise to free up the semaphore. utils.say("acquiring the semaphore...") semaphore.acquire() s = utils.read_from_memory(memory) while s == what_i_wrote: if not params["LIVE_DANGEROUSLY"]: # Release the semaphore... utils.say("releasing the semaphore") semaphore.release() # ...and wait for it to become available again. utils.say("acquiring for the semaphore...") semaphore.acquire() s = utils.read_from_memory(memory) if what_i_wrote: if PY_MAJOR_VERSION > 2: what_i_wrote = what_i_wrote.encode() try: assert(s == hashlib.md5(what_i_wrote).hexdigest()) except AssertionError: raise AssertionError("Shared memory corruption after %d iterations." % i) if PY_MAJOR_VERSION > 2: s = s.encode() what_i_wrote = hashlib.md5(s).hexdigest() utils.write_to_memory(memory, what_i_wrote) if not params["LIVE_DANGEROUSLY"]: utils.say("releasing the semaphore") semaphore.release() # TODO: remove time.sleep(1)
def wait_for_new_message(initial_message, mapfile, semaphore): message_i_just_read = initial_message while message_i_just_read == initial_message: semaphore.acquire() message_i_just_read = utils.read_from_memory(mapfile) semaphore.release() time.sleep(0.001) return json.loads(message_i_just_read)
def wait_for_new_message(initial_message, mapfile, semaphore): message_i_just_read = initial_message while message_i_just_read == initial_message: semaphore.acquire() message_i_just_read = utils.read_from_memory(mapfile) if not message_i_just_read == initial_message: utils.write_to_memory(mapfile, utils.READY_FOR_NEXT) semaphore.release() time.sleep(0.001) return message_i_just_read
def main(semaphore, mapfile, what_i_wrote =""): vehicle = connection_uav() semaphore.release() semaphore.acquire() s = utils.read_from_memory(mapfile) print s # I keep checking the shared memory until something new has # been written. while s == what_i_wrote: # Nothing new; give Mrs. Conclusion another chance to respond. utils.say("Releasing the semaphore") semaphore.release() utils.say("Waiting to acquire the semaphore") semaphore.acquire() s = utils.read_from_memory(mapfile) what_i_wrote = vehicle.attitude utils.write_to_memory(mapfile, what_i_wrote) return what_i_wrote
def run(self): try: memory = posix_ipc.SharedMemory('sixaxis_controller_1') mapfile = mmap.mmap(memory.fd, memory.size) os.close(memory.fd) for i in range(0, 1000): sleep(0.1) i += 1 s = utils.read_from_memory(mapfile, 100) x = struct.unpack('B', s[9])[0] y = struct.unpack('B', s[10])[0] self.valueUpdated.emit(int(x), int(y)) except: e = sys.exc_info()[0] print 'Error: %s' % e
def Process(self): # I seed the shared memory with a random value which is the current time. what_i_wrote = time.asctime() s = what_i_wrote #utils.write_to_memory(self.Memory, what_i_wrote) utils.say("iteration %d" % self.Cntr) self.Cntr += 1 if not self.Params["LIVE_DANGEROUSLY"]: # Releasing the semaphore... utils.say("releasing the semaphore") self.Semaphore.release() utils.say("acquiring the semaphore...") self.Semaphore.acquire() self.DispatchMsg(utils.read_from_memory(self.Memory)) if self.State == "IDLE": pass elif self.State == "INCR": pass elif self.State == "DECR": pass elif self.State == "QUIT": if not self.Params["LIVE_DANGEROUSLY"]: utils.say( "Final release of the semaphore followed by a 5 second pause" ) self.Semaphore.release() time.sleep(5) # ...before beginning to wait until it is free again. utils.say("Final acquisition of the semaphore") self.Semaphore.acquire() utils.say("Destroying semaphore and shared memory") # It'd be more natural to call memory.remove() and semaphore.remove() here, # but I'll use the module-level functions instead to demonstrate their use. sysv_ipc.remove_shared_memory(self.Memory.id) sysv_ipc.remove_semaphore(self.Semaphore.id) else: sysv_ipc.remove_shared_memory(self.Memory.id) sysv_ipc.remove_semaphore(self.Semaphore.id)
def wait_for_new_message2(mapfile, semaphore): new_message_found = False while not new_message_found: semaphore.acquire() message_i_just_read = utils.read_from_memory(mapfile) json_i_just_read = json_to_obj(message_i_just_read) if json_i_just_read['author'] == process_two_name and json_i_just_read['receiver'] == author_process: # message_to_send = { # 'author': author_process, # 'receiver': process_one_name, # 'action': reply_code # } # if last_message: # message_to_send['action'] = LAST_MESSAGE # utils.write_to_memory(mapfile, message_to_send) new_message_found = True semaphore.release() time.sleep(0.001) return json_i_just_read
def run(self): try: memory = posix_ipc.SharedMemory('sixaxis_controller_1') mapfile = mmap.mmap(memory.fd, memory.size) os.close(memory.fd) # for i in range(0, 1000): while True : sleep(0.1) s = utils.read_from_memory(mapfile, 100) a = struct.unpack('B', s[9])[0] z = struct.unpack('B', s[10])[0] x = struct.unpack('B', s[7])[0] y = struct.unpack('B', s[8])[0] b1 = struct.unpack('B', s[3])[0] b2 = struct.unpack('B', s[4])[0] b3 = struct.unpack('B', s[5])[0] l3 = 1 if b1 & 2 else 0 r3 = 1 if b1 & 4 else 0 start = 1 if b1 & 0x08 else 0 select = 1 if b1 & 0x01 else 0 ps = 1 if b3 & 0x01 else 0 up = struct.unpack('B', s[15])[0] right = struct.unpack('B', s[16])[0] down = struct.unpack('B', s[17])[0] left = struct.unpack('B', s[18])[0] l2 = struct.unpack('B', s[19])[0] r2 = struct.unpack('B', s[20])[0] l1 = struct.unpack('B', s[21])[0] r1 = struct.unpack('B', s[22])[0] tri = struct.unpack('B', s[23])[0] cir = struct.unpack('B', s[24])[0] cro = struct.unpack('B', s[25])[0] squ = struct.unpack('B', s[26])[0] self.input.emit(a,z,x,y,l3,r3,start,select,ps,up,right,down,left,tri,cir,squ,cro,l2,r2,l1,r1) # self.controller.update() # print 'b1:%d b2:%d' % (b1, b2) except: e = sys.exc_info()[0] print 'Error: %s' % e
def wait_for_new_message2(reply_code, mapfile, semaphore, last_message=False): new_message_found = False while not new_message_found: semaphore.acquire() message_i_just_read = utils.read_from_memory(mapfile) json_i_just_read = json_to_obj(message_i_just_read) if json_i_just_read['author'] == process_one_name and json_i_just_read[ 'receiver'] == author_process: message_to_send = { 'author': author_process, 'receiver': process_one_name, 'action': reply_code } # print ("got message:") # print json_i_just_read if last_message: message_to_send['action'] = LAST_MESSAGE utils.write_to_memory(mapfile, json.dumps(message_to_send)) new_message_found = True semaphore.release() time.sleep(0.001) return json_i_just_read
def run(self): try: memory = posix_ipc.SharedMemory('sixaxis_controller_1') mapfile = mmap.mmap(memory.fd, memory.size) os.close(memory.fd) for i in range(0, 1000): sleep(0.1) i += 1 s = utils.read_from_memory(mapfile, 100) a = struct.unpack('B', s[9])[0] z = struct.unpack('B', s[10])[0] x = struct.unpack('B', s[7])[0] y = struct.unpack('B', s[8])[0] b1 = struct.unpack('B', s[3])[0] b2 = struct.unpack('B', s[4])[0] b3 = struct.unpack('B', s[5])[0] l3 = 1 if b1 & 2 else 0 r3 = 1 if b1 & 4 else 0 self.valueUpdated.emit(x, y, a, z, l3, r3) print 'b1:%d b2:%d' % (b1, b2) except: e = sys.exc_info()[0] print 'Error: %s' % e
# Once I've mmapped the file descriptor, I can close it without # interfering with the mmap. This also demonstrates that os.close() is a # perfectly legitimate alternative to the SharedMemory's close_fd() method. os.close(memory.fd) what_i_wrote = "" for i in range(0, params["ITERATIONS"]): utils.say("iteration %d" % i) if not params["LIVE_DANGEROUSLY"]: # Wait for Mrs. Premise to free up the semaphore. utils.say("Waiting to acquire the semaphore") semaphore.acquire() s = utils.read_from_memory(mapfile) while s == what_i_wrote: if not params["LIVE_DANGEROUSLY"]: # Release the semaphore... utils.say("Releasing the semaphore") semaphore.release() # ...and wait for it to become available again. utils.say("Waiting to acquire the semaphore") semaphore.acquire() s = utils.read_from_memory(mapfile) if what_i_wrote: if PY_MAJOR_VERSION > 2: what_i_wrote = what_i_wrote.encode()
for i in range(0, params["ITERATIONS"]): utils.say("iteration %d" % i) if not params["LIVE_DANGEROUSLY"]: # Releasing the semaphore... utils.say("releasing the semaphore") semaphore.release() # ...and wait for it to become available again. In real code it'd be # wise to sleep briefly before calling .acquire() in order to be # polite and give other processes an opportunity to grab the semaphore # while it is free and thereby avoid starvation. But this code is meant # to be a stress test that maximizes the opportunity for shared memory # corruption, and politeness has no place in that. utils.say("acquiring the semaphore...") semaphore.acquire() s = utils.read_from_memory(memory) # I keep checking the shared memory until something new has been written. while s == what_i_wrote: if not params["LIVE_DANGEROUSLY"]: utils.say("releasing the semaphore") 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. what_i_wrote = what_i_wrote.encode()
for i in range(0, params["ITERATIONS"]): utils.say("iteration %d" % i) if not params["LIVE_DANGEROUSLY"]: # Releasing the semaphore... utils.say("releasing the semaphore") semaphore.release() # ...and wait for it to become available again. In real code it'd be # wise to sleep briefly before calling .acquire() in order to be # polite and give other processes an opportunity to grab the semaphore # while it is free and thereby avoid starvation. But this code is meant # to be a stress test that maximizes the opportunity for shared memory # corruption, and politeness has no place in that. utils.say("acquiring the semaphore...") semaphore.acquire() s = utils.read_from_memory(memory) # I keep checking the shared memory until something new has been written. while s == what_i_wrote: if not params["LIVE_DANGEROUSLY"]: utils.say("releasing the semaphore") 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:
# starting_message = utils.FIRST_HANDSHAKE utils.write_to_memory(mapfile, starting_message) message_i_just_read = starting_message print("Waiting for second process") waiting_time = 0 while message_i_just_read == starting_message: sys.stdout.write("\r{0}".format("."*waiting_time)) sys.stdout.flush() semaphore.release() time.sleep(1) waiting_time+=1 semaphore.acquire() message_i_just_read = utils.read_from_memory(mapfile) semaphore.release() print("") print("I feel second process is ready, let's roll") time.sleep(1) cam = start_camera() # meta, focused_recs, focused_mod, to_console = process_image_get_points(cam,[], False) # meta_json = json.dumps(meta) # utils.write_to_memory(mapfile, meta_json) # utils.write_to_memory(mapfile, utils.THIRD_HANDSHAKE) start_time = time.time() frame_counter = 0
for faces in meta['points']: for p in faces: draw_point(blank_image, (p[0],p[1]), (0,0,255)) # cv2.imshow('image',blank_image) # cv2.waitKey(1) blank_image = cv2.resize(blank_image,None,fx=(1/utils.SCALE_DOWN), fy=(1/utils.SCALE_DOWN), interpolation = cv2.INTER_CUBIC) return blank_image if __name__ == '__main__': shared_memory, semaphore = access_shared_memory_and_semaphore() mapfile = mmap.mmap(shared_memory.fd, shared_memory.size) print("Starting the process") time.sleep(1) semaphore.acquire() first_readed_message = utils.read_from_memory(mapfile) if not first_readed_message == utils.FIRST_HANDSHAKE : print("brr, some bullshit here") else: print("Passing back the handshake") utils.write_to_memory(mapfile, utils.SECOND_HANDSHAKE) semaphore.release() message_i_just_read = utils.SECOND_HANDSHAKE # message_i_just_read = wait_for_new_message(message_i_just_read, mapfile, semaphore, False) while True: message_i_just_read = wait_for_new_message(message_i_just_read, mapfile, semaphore) if message_i_just_read == utils.THIRD_HANDSHAKE or message_i_just_read== utils.SECOND_HANDSHAKE: continue