import os import string import mmap, struct import binascii import win32api, win32event class SharedMemory: def __init__(self, memory_name, lock_timeout = 10000): self.memory_name = memory_name # Shared memory name self.lock_timeout = lock_timeout # Shared memory lock wait time self.is_creator = False # Am I the creator of the shared memory? self.memory = None # Shared memory object self.memory_size = 0 # Total shared memory size self.mutex_name = memory_name + '_mutex' # Mutex name self.hmutex = None # Mutex object self.user_lock_on = False # Shared memory user lock on? # Create or open the mutex object self.hmutex = win32event.CreateMutex(None, 0, self.mutex_name) # start of __init__() for memory mapped file if (win32api.GetLastError() == 183): # Mutex object already exists. Just open the existing # mapping-file object or restore_file. self.__open() else: # Create a new mapping-file object self.__create() # __init__() def __create(self): """Create the mapping-file object""" # Creator of the shared memory self.is_creator = True #hardwire for test num_1 = 40 num_2 = 20 num_3 = 10 # Calcuate total shared memory size isize = struct.calcsize('i') memsize = isize * (4 + 2*num_1) # Wait for mutex before creating the mapping-file object self.__get_mutex() # Create mapping-file object self.memory_size = memsize self.memory = mmap.mmap(-1, memsize, self.memory_name) # Write first items in memory self.memory.write(struct.pack('iiii', num_1, memsize, num_2,num_3)) # Release mutex object self.__release_mutex() # create() def __open(self): """Open the mapping-file object""" # Calculate size of an integer isize = struct.calcsize('i') # Wait for mutex before opening the mapping-file object self.__get_mutex() # Read total shared memory size, which is given by the second # integer in the shared memory. self.memory = mmap.mmap(-1, (2*isize), self.memory_name) ##self.memory = mmap.mmap(-1, 0, self.memory_name) # doesn't work either self.memory_size = struct.unpack("i", self.memory[isize:(2*isize)])[0] if self.memory_size == 0: print "Error - Shared memory has not been created" sys.exit() print "open -self.memory_size " + str(self.memory_size) # Now map in the entire shared memory self.memory.resize(self.memory_size) # Release mutex object self.__release_mutex() # open() def lock(self): """Lock the shared memory""" if not self.user_lock_on: # Do not change the order of following two statements self.__get_mutex() self.user_lock_on = True # lock() def unlock(self): """Unlock the shared memory""" if self.user_lock_on: # Do not change the order of following two statements self.user_lock_on = False self.__release_mutex() # unlock() def __get_mutex(self): """Get the mutex object""" if self.user_lock_on: # Let user explicitly handle the lock return rc = win32event.WaitForSingleObject(self.hmutex, self.lock_timeout) if (rc == win32event.WAIT_TIMEOUT): print 'Error - Mutex timeout' sys.exit() # __get_mutex() def __release_mutex(self): """Release the mutex object""" if self.user_lock_on: # Let user explicitly handle the lock return win32event.ReleaseMutex(self.hmutex) # __release_mutex() def example(): print 'Testing SharedMemory' import os import time # Create or open SharedMemory object sm = SharedMemory( 'my_shared_memory') if sm.is_creator: print 'I am the creator of the shared memory' print sm.memory_size else: print 'I am not the creator of the shared memory' counter = 0 while True: counter = counter + 1 print counter time.sleep(1) # example() if __name__ == '__main__': try: example() except KeyboardInterrupt: print 'Goodbye!' # End of file