python和zmq包实现在sever和client之间进行信息传递

版权声明:本文为博主原创文章,转载请注明来源 https://blog.csdn.net/qq_26948675/article/details/91126396

zmq包

这个包用于在sever和client之间传递信息挺方便的,比socket用着简单。

一个使用场景

在量化平台中,如果需要跑多个策略,每个策略都需要获取数据的时候,如果都从交易所获取,可能超过请求限制,可以在本地只获取一次,获取的数据放到sever上,每个策略作为client调用sever的数据,这样就解决了数据限制的问题。

小例子

server

import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5411")

while True:
    message={'name':'yunjinqi','time':time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())}
    print(message)
    socket.send_string(str(message))
    time.sleep(1)

client

import zmq
import sys

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:5411")
socket.setsockopt_string(zmq.SUBSCRIBE,'')  # 消息过滤
while True:
    response = socket.recv();
    print( eval(bytes.decode(response)))

文章来自于知乎专栏:https://zhuanlan.zhihu.com/p/68330525

猜你喜欢

转载自blog.csdn.net/qq_26948675/article/details/91126396