Administrator
28 分钟以前 2f2516749615da866e96d8d24e499b7ecbb63a3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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_