Introduction to pyzmq

Time:2020-11-29

Introduction to pyzmq

ZMQ (hereinafter referred to as ZMQ) is a simple and easy-to-use transport layer, like a socket library framework, it makes socket programming more simple, concise and higher performance.

Is a message processing queue library that can be flexibly scaled between multiple threads, kernel and mainframe. ZMQ’s clear goal is to “become part of the standard network protocol stack, and then enter the Linux kernel.”.

ZMQ makes writing high-performance web applications extremely simple and fun.

Zeromq is not an encapsulation of socket, so it can not be used to implement the existing network protocol.

It has its own mode, which is different from the lower level point-to-point communication mode.

It has a higher level of protocol than TCP. (of course, zeromq is not necessarily based on the TCP protocol, it can also be used for inter process and intra process communication)

zeromqIt’s not like thatrabbitmqMessage queuing, which actually has only one message queuing component and one library.

Download

pip install pyzmq

Several modes of pyzmq

1. Request reply (rep and req)

The message is bidirectional. The rep side must reply to the req side for the message requested by the req side

2. Subscription publishing mode (pub and sub)

The message is one-way, there is no return. According to the publisher, a message can be published to set a topic. A subscriber can subscribe to a favorite topic, and the subscriber will only receive the topic that he has subscribed to. The publisher publishes a message that can be received by multiple subscriber colleagues.

3. Push pull mode

The message is one-way, and there is no return. For any message of push, only one pull side will receive the message

The following proxy mode and routing mode are the extension or variation of the three basic modes.

1. Request reply mode (request response model)

After the client requests, the server must respond

The client initiates the request and waits for the server to respond to the request. From the point of view of the client, it must be a pair of sending and receiving pairs;

On the contrary, it must be a transceiver on the server. Both the server and the client can be a 1: n model. Generally, 1 is considered as server and N as client.

ZMQ can well support the routing function (the component that implements the routing function is called device), and extends 1: n to N: m (only a few routing nodes need to be added).

From this model, the lower endpoint address is hidden from the upper layer. Each request implies a response address, and the application doesn’t care about it

Introduction to pyzmq

Server:

sever.py

 import zmq
 import sys
 context = zmq.Context()
 socket = context.socket(zmq.REP)
 socket.bind("tcp://*:5555")
 while True:
  try:
  print("wait for client ...")
  message = socket.recv()
  print("message from client:", message.decode('utf-8'))
  socket.send(message)
  except Exception as e:
  print('exception:',e)
  sys.exit()

client:

#client.py

 import zmq
 import sys
 context = zmq.Context()
 print("Connecting to server...")
 socket = context.socket(zmq.REQ)
 socket.connect("tcp://localhost:5555")
 while True:
 ​
  input1 = input(Please enter:).strip()
  if input1 == 'b':
  sys.exit()
  socket.send(input1.encode('utf-8'))
 ​
  message = socket.recv()
  print("Received reply: ", message.decode('utf-8'))

2. Publish subscribe model:

Broadcast all clients, no queue cache, disconnect data will be lost forever. Client can filter data.

Server

server.py

 import zmq
 import time
 import sys
 context = zmq.Context()
 socket = context.socket(zmq.PUB)
 socket.bind("tcp://*:5555")while True:
  msg = input(Please enter information to publish:).strip()
  if msg == 'b':
  sys.exit()
  socket.send(msg.encode('utf-8'))
  time.sleep(1)

Client 1

client1.py

 import zmq
 ​
 ​
 context = zmq.Context()
 socket = context.socket(zmq.SUB)
 socket.connect("tcp://localhost:5555")
 socket.setsockopt(zmq.SUBSCRIBE,''.encode('utf-8'))#Receive all messageswhile True:
  response = socket.recv().decode('utf-8');
  print("response: %s" % response)

Client 2

client2.py

 import zmq
 context = zmq.Context()
 socket = context.socket(zmq.SUB)
 socket.connect("tcp://localhost:5555")
 socket.setsockopt(zmq.SUBSCRIBE,'123'.encode('utf-8'))#Message filtering only accepts123Information at the beginningwhile True:
  response = socket.recv().decode('utf-8');
  print("response: %s" % response)

3. Parallel pipeline mode (pipeline model)

It consists of three parts: push to push data, work to cache data, pull to compete for data acquisition. Unlike publish subscribe, there is a data cache and processing load.

When the connection is disconnected, the data will not be lost. After reconnection, the data will continue to be sent to the opposite end.

Introduction to pyzmq

server.py

 import zmq
 import time
 ​
 context = zmq.Context()
 socket = context.socket(zmq.PUSH)
 socket.bind("tcp://*:5557")while True:
  msg = input(Please enter information to publish:).strip()
  socket.send(msg.encode('utf-8'))
  print("Sent")
  time.sleep(1)

worker.py

 import zmq
 context = zmq.Context()
 receive = context.socket(zmq.PULL)
 receive.connect('tcp://127.0.0.1:5557')
 sender = context.socket(zmq.PUSH)
 sender.connect('tcp://127.0.0.1:5558')while True:
  data = receive.recv()
  print(Forwarding)
  sender.send(data)

client.py

 import zmq
 context = zmq.Context()
 socket = context.socket(zmq.PULL)
 socket.bind("tcp://*:5558")while True:
  response = socket.recv().decode('utf-8')
  print("response: %s" % response)

reference resources:www.cnblogs.com/yunwangjun-python-…

This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint

Recommended Today

PHP 12th week function learning record

sha1() effect sha1()Function to evaluate the value of a stringSHA-1Hash. usage sha1(string,raw) case <?php $str = “Hello”; echo sha1($str); ?> result f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 sha1_file() effect sha1_file()Function calculation fileSHA-1Hash. usage sha1_file(file,raw) case <?php $filename = “test.txt”; $sha1file = sha1_file($filename); echo $sha1file; ?> result aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d similar_text() effect similar_text()Function to calculate the similarity between two strings. usage similar_text(string1,string2,percent) case […]