import multiprocessing
|
import time
|
|
import msgpack
|
|
|
class L2ChannelCommunicationParams:
|
"""
|
L2通信参数
|
"""
|
|
def __init__(self, delegate_ipc_addr: str, deal_ipc_addr: str, delegate_data_shared_memory: multiprocessing.Array,
|
deal_data_shared_memory: multiprocessing.Array):
|
"""
|
初始化参数
|
@param delegate_ipc_addr: 逐笔委托ipc地址
|
@param deal_ipc_addr: 逐笔成交ipc地址
|
@param delegate_data_shared_memroy: 逐笔委托数据共享内存
|
@param deal_data_shared_memroy: 逐笔成交数据共享内存
|
"""
|
self.delegate_ipc_addr = delegate_ipc_addr
|
self.deal_ipc_addr = deal_ipc_addr
|
self.delegate_data_shared_memory = delegate_data_shared_memory
|
self.deal_data_shared_memory = deal_data_shared_memory
|
|
|
class L2SharedMemoryDataUtil:
|
"""
|
L2共享内存数据帮助
|
"""
|
|
@classmethod
|
def create_shared_memory(cls, size = 1024 * 1024):
|
return multiprocessing.Array('i',size)
|
|
@classmethod
|
def set_data(cls, data, array: multiprocessing.Array):
|
"""
|
将数据设置进入共享内存
|
@param data: 设置的数据,设置数据最大为99994字节
|
@param array: 目标地址
|
@return: 数据设置耗时
|
"""
|
time_ = time.time()
|
str_ = msgpack.packb(data)
|
lenth_count = 5
|
str_lenth = "{0:0>5}".format(len(str_))
|
lenth_bytes = str_lenth.encode('utf-8')
|
array[0:lenth_count] = lenth_bytes
|
array[lenth_count:len(str_) + lenth_count] = str_
|
return time.time() - time_
|
|
@classmethod
|
def get_data(cls, array: multiprocessing.Array):
|
"""
|
获取共享内存数据
|
@param array: 内存地址
|
@return: 数据,耗时
|
"""
|
time_ = time.time()
|
lenth_count = 5
|
raw_data = array.get_obj()
|
data_lenth = int(bytes(raw_data[:lenth_count]).decode('utf-8'))
|
data = raw_data[lenth_count:data_lenth + lenth_count]
|
data = msgpack.unpackb(bytes(data))
|
return data, time.time() - time_
|