Created
December 11, 2023 15:43
-
-
Save kylemcdonald/b8f2cf00d02730df7ae86bdaf69a8e8b to your computer and use it in GitHub Desktop.
PUSH-PULL pattern with ZMQ.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
context = zmq.Context() | |
socket = context.socket(zmq.PULL) | |
socket.bind("tcp://*:5558") | |
print("Collector started... collecting results.") | |
while True: | |
result = socket.recv_json() | |
print(f"Collected: {result}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import time | |
import random | |
context = zmq.Context() | |
socket = context.socket(zmq.PUSH) | |
socket.bind("tcp://*:5557") | |
print("Producer started... producing jobs.") | |
while True: | |
job = random.randrange(1, 100) # Randomly generate a simple job | |
socket.send_json({"job_id": job}) | |
print(f"Sent job #{job}") | |
time.sleep(0.1) # Simulate time between sending jobs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import time | |
import random | |
worker_id = random.randrange(1, 1000) | |
context = zmq.Context() | |
# Connect to the producer's job dispatch | |
receiver = context.socket(zmq.PULL) | |
receiver.connect("tcp://localhost:5557") | |
# Connect to the collector to send results | |
sender = context.socket(zmq.PUSH) | |
sender.connect("tcp://localhost:5558") | |
print(f"Worker #{worker_id} started... pulling jobs.") | |
while True: | |
job = receiver.recv_json() # Receive job from producer | |
job_id = job["job_id"] | |
print(f"Processing job #{job_id}") | |
time.sleep(0.2) # Simulate some work being done | |
# Once complete, send results to the collector | |
sender.send_json({"job_id": job_id, "worker": worker_id}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment