import mmap import contextlib import multiprocessing import time def run_process_1(pipe): with contextlib.closing(mmap.mmap(-1, 1000 * 100, tagname='l2-000333', access=mmap.ACCESS_WRITE)) as m: for i in range(1, 10001): start = time.time() m.seek(0) m.write((f"msg {i} " * 10000).encode("utf-8")) m.flush() print("耗时", time.time() - start) time.sleep(1) def run_process_2(pipe): while True: with contextlib.closing(mmap.mmap(-1, 1000 * 100, tagname='l2-000333', access=mmap.ACCESS_READ)) as m: s = m.read(1000 * 100) s = s.decode('utf-8').replace('\x00', '') if s: print(len(s), s) time.sleep(1) if __name__ == '__main__': p1, p2 = multiprocessing.Pipe() serverProcess = multiprocessing.Process(target=run_process_1, args=(p1,)) jueJinProcess = multiprocessing.Process(target=run_process_2, args=(p2,)) serverProcess.start() jueJinProcess.start() while True: time.sleep(2)