def main(): smm = SharedMemoryManager() smm.start() ls = smm.ShareableList(range(2000)) with Pool(4) as p: print(*list(p.imap_unordered(f, ls)), sep='\n') # print(p.map(f, [2, 3, 4, 5, 6])) # lock # print(p.map(f, [2, 3, 4, 5, 6])) smm.shutdown()
def lif_feed_forward_benchmark(parameters: BenchmarkParameters): shared = SharedMemoryManager() shared.start() params = list(parameters._asdict().values()) shared_list = shared.ShareableList(params) run(["python3", __file__, shared_list.shm.name], stderr=STDOUT) duration = shared_list[0] shared_list.shm.close() shared.shutdown() return duration
def multiprocess(self, n, nthread=8): """ 多进程 并发 :return: """ print('Parent process %s.' % os.getpid()) p = Pool(nthread) # 进程池, 和系统申请 nthread 个进程 smm = SharedMemoryManager() #TODO: pyrhon3.8+ 才有 smm.start() # Start the process that manages the shared memory blocks cache_list = smm.ShareableList([0] * n) # 限制了可被存储在其中的值只能是 int, float, bool, str (每条数据小于10M), bytes (每条数据小于10M)以及 None 这些内置类型。 # 它另一个显著区别于内置 list 类型的地方在于它的长度无法修改(比如,没有 append, insert 等操作) # 且不支持通过切片操作动态创建新的 ShareableList 实例。 shm_a = smm.SharedMemory(size=n) # shm_a.buf[:] = bytearray([0]*n) # shm_a.buf[:] = [0] * n print('shm_a id in main process: {} '.format(id(shm_a))) # 主进程的内存空间 和 子进程的内存空间 的考察 self.global_array = [0] * n print('array id in main process: {} '.format(id(self.global_array))) self.global_string = 'abc' print('string id in main process: {} '.format(id(self.global_string))) self.global_int = 10 print('int id in main process: {} '.format(id(self.global_int))) for i in range(n): # p.apply_async(task, args=(cache_name,i)) # apply_async 异步取回结果 p.apply_async(self.task, args=(cache_list, shm_a, i)) print('Waiting for all subprocesses done...') p.close() p.join() print('All subprocesses done.') smm.shutdown() return cache_list, shm_a
def main(): # This is the number of values that the writer will send to the reader items_to_send = random.randint(100000, 1000000) smm = SharedMemoryManager() smm.start() # Create a ShareableList to be used between the processes size = BUFFER_SIZE + 2 shared_list = smm.ShareableList(range(size)) for i in range(0, size): shared_list[i] = -1 # TODO - Create any lock(s) or semaphore(s) that you feel you need lock = mp.Lock() sem = mp.Semaphore(BUFFER_SIZE) # TODO - create reader and writer processes items_sent = 0 items_read = 0 p1 = mp.Process(target=write, args=(shared_list, items_sent, items_to_send, sem, lock)) p2 = mp.Process(target=read, args=(shared_list, items_read, items_to_send, sem, lock)) # TODO - Start the processes and wait for them to finish p1.start() p2.start() p1.join() p2.join() print(f'{items_to_send} sent by the writer') # TODO - Display the number of numbers/items received by the reader. print(f'{shared_list[-1]} read by the reader') smm.shutdown()
class SMBase: def __init__(self, addr=None, manager=None, mutex=None, format_list=None, size=0, ratio=2): if mutex is None: self.mutex = RLock() else: self.mutex = mutex if manager is None: self._manager = DummyManager() elif isinstance(manager, SharedMemoryManager) or isinstance(manager, DummyManager): self._manager = manager else: self._manager = SharedMemoryManager(manager) capacity = int(size*ratio) if capacity == 0: capacity = ratio with self.mutex: if addr is None: if format_list is None: raise ValueError("Either addr or format_list must be provided") self._shl = self._manager.ShareableList(format_list) self._shl_addr = self._shl.shm.name self._shm = self._manager.SharedMemory(capacity) self._shm_addr = self._shm.name self._shl[0] = self._shm_addr self._shl[1] = int(size) self._shl[2] = int(capacity) else: self._shl_addr = addr self._shl = shared_memory.ShareableList(name=addr) self._shm_addr = self._shl[0] self._shm = shared_memory.SharedMemory(name=self._shm_addr) @locked def size(self): return self._shl[1] @locked def capacity(self): return self._shl[2] @locked def name(self): return self._shl.shm.name def check_memory(self) -> bool: updated = False with self.mutex: if self._shm_addr != self._shl[0]: updated = True try: self._shm.close() except: traceback.print_exc() self._shm = shared_memory.SharedMemory(name=self._shl[0]) self._shm_addr = self._shl[0] return updated def _recap(self, cap: int): if cap > self._shm.size: new_shm = self._manager.SharedMemory(size=int(cap)) new_shm.buf[:self._shm.size] = self._shm.buf[:] try: self._shm.close() self._shm.unlink() except: traceback.print_exc() self._shm = new_shm self._shm_addr = new_shm.name self._shl[0] = new_shm.name self._shl[2] = int(cap) def __del__(self): """ Note: this does not unlink data. That is expected to be handled by the manager. """ self._shm.close() self._shl.shm.close()
from multiprocessing.managers import SharedMemoryManager from multiprocessing.shared_memory import ShareableList from multiprocessing import Process import multiprocessing # Instanciamos un gestor de bloques memoria compartida smm = SharedMemoryManager() # iniciamos el proceso smm.start() # Creamos una lista compartida con strings lc = smm.ShareableList(["hola", "BlaBlaBlaBla", "BlaBlaBlaBLa"]) #Imprimimos la lista para ver su estado actual print('Este es el estado inicial de la lista: \n' + str(lc)) #Creamos la funcion que realizaran los procesos que consumiran la memoria compartida #Estas funciones son creadas para cada proceso, cambian un string de la lista y la imprimen def workerProceso1(): #Obtenemos la lista por su nombre a = ShareableList(name=lc.shm.name) #Agregamos a la lista un string a[1] = "Proceso 1" print('\n hola soy el proceso 1 \n' + 'Imprimiendo la lista compartida \n' + str(a)) def workerProceso2(): #Obtenemos la lista por su nombre