个人理解的定义
高效、低延迟、可重用的消息传递系统(或消息传递层,有点类型网络协议,但不是消息队列)
基于python3和ZMQ做进程间通信示例:
# 本脚本用于从zmq队列中接收订阅的数据
import time
import json
import zmq
def subscriber(code: str):
# Create a new ZeroMQ context
context = zmq.Context()
# Create a SUB socket
socket = context.socket(zmq.SUB)
# Connect to the publisher's address
socket.connect("tcp://localhost:5555")
# Subscribe to all messages (empty string as a subscription)
socket.setsockopt_string(zmq.SUBSCRIBE, "")
while True:
# Receive a message
message = socket.recv_string()
# print('message=%s' % message)
msg_dict: dict = json.loads(message)
if msg_dict['type'] == 'MarketData':
index_data_str = msg_dict['body']
try:
# # 打印字符串长度和内容
# print("String length:", len(index_data_str))
# print("String content:", index_data_str)
# 解析 JSON 字符串
data = json.loads(index_data_str)
# 提取指数的合约的代码
instrument_str = data.get('instrument')
if instrument_str == code:
# 提取 'highestPrice' 字段的值
lastPrice = data.get('lastPrice')
print('instrument', instrument_str)
print("lastPrice Price:", lastPrice)
except json.JSONDecodeError as e:
print(f"JSON decode error: {e}")
if __name__ == "__main__":
# 三大指数
subscriber('000300')
subscriber('000905')
subscriber('000852')
GitHubID:shiyi23
践行 活在当下 的理念