跨进程读写内存想要高效,肯定是能不加锁就不加锁,进程锁对效率有很大影响。
通常进程之间交换数据最快就是共享内存了,于是写了关于如下测试程序,只是实现一个进程写,一个进程读的单生产单消费者模型
测试不休息,一秒可传输30万次,虽然不如线程之间高效,但在进程之间还可以吧
import time
import random
from multiprocessing import Process
import mmap
class Write(Process):
def __init__(self,name,share_memname):
super().__init__()
self.name=name
self.share_memname = share_memname
self.share_memlen= 100
def run(self):
self.process_run_init()
start_time = time.time()
for i in range(0,100000):
bytes_buffer = str(i).encode('UTF-8')
self.write(bytes_buffer)
self.wait_read()
end_time = time.time()
print(end_time-start_time)
def write(self,content):
self.write_content(content)
self.write_finish_flags()
def wait_read(self):
while True:
self.mapfile.seek(0)
flags = self.mapfile.read(1)
if flags == b'1':
pass
#time.sleep(0.5)
elif flags == b'0':
return
def process_run_init(self):
self.mapfile = mmap.mmap(
fileno = -1,
length = self.share_memlen,
access=mmap.ACCESS_DEFAULT,
tagname=self.share_memname
)
def write_headers(self,headers):
pass
def write_content(self,content):
self.mapfile.seek(1)
self.mapfile.write(content)
def write_finish_flags(self):
self.mapfile.seek(0)
self.mapfile.write(b'1')
class Read(Process):
def __init__(self, name,share_memname):
super().__init__()
self.name = name
self.share_memname = share_memname
self.share_memlen= 100
def run(self):
self.process_run_init()
while True:
self.wait_write()
self.read()
def process_run_init(self):
self.mapfile = mmap.mmap(
fileno = -1,
length = self.share_memlen,
access=mmap.ACCESS_DEFAULT,
tagname=self.share_memname
)
def wait_write(self):
while True:
self.mapfile.seek(0)
flags = self.mapfile.read(1)
if flags == b'1':
return
elif flags == b'0':
pass
#time.sleep(0.1)
def read(self):
self.read_content()
self.write_finish_flags()
def read_content(self):
self.mapfile.seek(1)
buffer = self.mapfile.read()
print(buffer)
def write_finish_flags(self):
self.mapfile.seek(0)
self.mapfile.write(b'0')
p1=Write('Task',share_memname = 'kkkkkk')
p2=Read('Exec',share_memname = 'kkkkkk')
if __name__ == '__main__':
p1.start() # start会自动调用run
p2.start()
print('主线程')
来源:CSDN
作者:shawff
链接:https://blog.csdn.net/qq_33913982/article/details/104344347