python and zmq package implements client and sever between information transmission

Disclaimer: This article is a blogger original article, please indicate the source https://blog.csdn.net/qq_26948675/article/details/91126396

zmq package

This package for the transmission of information between a client and sever very convenient, simpler than with the socket.

A usage scenario

In the quantification platform, if you need to run multiple policies, each policy will need time to acquire data, if all acquired from an exchange, may exceed the limit request, you can only get one locally acquired data onto sever, each as a client calls a strategy of data sever, this would resolve the problem of data limitations.

Small example

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)))

Column know almost from the article: https://zhuanlan.zhihu.com/p/68330525

 

Guess you like

Origin blog.csdn.net/qq_26948675/article/details/91126396
zmq