[Python for All] Three modes of communication in Python using ZeroMQ

Table of contents

1. Introduction

2. Install ZeroMQ

3. Mode 1 (publish-subscribe mode)

Detailed explanation of publish-subscribe model

Applicable scene:

Sample code:

4. Mode 2 (request-response mode)

Detailed explanation of request-response pattern

Applicable scene:

Sample code:    

5. Mode 2 (request-response mode)

Detailed explanation of push-pull mode

Applicable scene:

Sample code:


1. Introduction

ZeroMQ (ZMQ) is an efficient, lightweight messaging library widely used for building distributed systems and network communications. This tutorial will introduce how to use Python and ZeroMQ to create a simple messaging system, including publish-subscribe, request-response, and push-pull modes.

2. Install ZeroMQ

pip install pyzmq

3. Mode 1 (publish-subscribe mode)

Detailed explanation of publish-subscribe model

In the publish-subscribe model, it is suitable for one-to-many communication scenarios. A publisher sends messages and multiple subscribers receive messages.

Applicable scene:

  • News subscription system: A news publisher publishes news regularly, and multiple users receive news notifications as subscribers.

Sample code:

# 发布者
import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://127.0.0.1:5555")

while True:
    news = "Breaking News! Time: {}".format(time.time())
    socket.send_string(news)
    time.sleep(1)
# 订阅者
import zmq

context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://127.0.0.1:5555")
socket.setsockopt_string(zmq.SUBSCRIBE, "")

while True:
    news = socket.recv_string()
    print("Received News: {}".format(news))

4. Mode 2 (request-response mode)

Detailed explanation of request-response pattern

In the request-response mode, it is suitable for scenarios where the client sends a request to the server and the server returns a response.

Applicable scene:

  • Client-server communication: The client sends a request, and the server processes the request and returns the corresponding result.

Sample code:    

# 请求者
import zmq

context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://127.0.0.1:5556")

while True:
    request = input("Enter your request: ")
    socket.send_string(request)
    
    response = socket.recv_string()
    print("Response: {}".format(response))
# 响应者
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://127.0.0.1:5556")

while True:
    request = socket.recv_string()
    print("Received request: {}".format(request))
    
    response = input("Enter your response: ")
    socket.send_string(response)

5. Mode 2 (request-response mode)

Detailed explanation of push-pull mode

In the push-pull model, it is suitable for scenarios where a single producer broadcasts messages to multiple consumers.

Applicable scene:

  • Task distribution system: A task distributor pushes tasks to multiple workers, and workers obtain tasks through pull methods for processing.

Sample code:

# 推送者
import zmq
import time

context = zmq.Context()
socket = context.socket(zmq.PUSH)
socket.bind("tcp://127.0.0.1:5557")

while True:
    task = "New task! Time: {}".format(time.time())
    socket.send_string(task)
    time.sleep(1)
# 拉取者
import zmq

context = zmq.Context()
socket = context.socket(zmq.PULL)
socket.connect("tcp://127.0.0.1:5557")

while True:
    task = socket.recv_string()
    print("Received Task: {}".format(task))

These examples provide basic scenarios and code for using publish-subscribe, request-response, and push-pull patterns. In practical applications, you can combine these modes according to your needs to create more complex and flexible communication systems.

Guess you like

Origin blog.csdn.net/qq_37310110/article/details/134800189