python-共享内存

通过 共享内存 和其他进程通信

try:
log.info('fireworks_plugin start')
shm = shared_memory.SharedMemory(create=False, size=mem_share_size, name='fireworksShareMemory')
with Pair0(dial=address) as s1:
s1.send(b'python login')
while True:
msg = s1.recv().decode(encoding='utf-8')
print('python:' + msg)
log.info('receive: ' + msg)
if msg == 'exit':
s1.send(b'python bye')
time.sleep(0.5)
log.info('exit nng loop')
break
elif msg.startswith('datas') or msg.startswith('ready'):
log.info('receive ' + msg)
# datas,12332 后面是共享内存数据大小
# load data process by memory mapping mmap file
# 收到的数据格式:"datas,size,cur index, all count"
param_list = msg.split(',')
if len(param_list) >= 4:
# 获取数据大小
data_size = int(param_list[1])
# 读取共享内存数据放到 share_df_list 里面
load_share_data(shm, data_size)
# 最后一组数据,接收完毕后可以处理
if param_list[2] == param_list[3]:
log.info('get last group data')
ret = proc_raw_data(share_df_list)
print('Python datas_RESULT,' + ''.join(ret))
log.info('datas_RESULT,' + ''.join(ret))
s1.send(bytes('datas_RESULT,' + ''.join(ret), encoding='gbk'))
else:
s1.send(b'datas_OK')
pass

# proc_raw_data()
shm.close()
shm.unlink() # Free and release the shared memory block at the very end
log.info('exit plugin')
except Exception as e:
import traceback
traceback.print_exc()
errMsg = traceback.format_exc()
print("Error: " + errMsg)
log.error("call plugin exception." + errMsg)
except:
import traceback

traceback.print_exc()
errMsg = traceback.format_exc()
print("Error: " + errMsg)
log.error("call plugin exception." + errMsg)