Created
May 27, 2021 14:03
-
-
Save jnyryan/9cf5f3662d30e753fba4f719f7b8f8fe to your computer and use it in GitHub Desktop.
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 time | |
import zmq | |
from multiprocessing import Process | |
def ventilator(): | |
context = zmq.Context() | |
ventilator_send = context.socket(zmq.PUSH) | |
ventilator_send.bind("tcp://127.0.0.1:5557") | |
# Give everything a second to spin up and connect | |
time.sleep(1) | |
# Send the numbers between 1 and ten thousand as work messages | |
for num in range(10000): | |
print('push msg') | |
work_message = { 'num' : num } | |
ventilator_send.send_json(work_message) | |
time.sleep(1) | |
def worker(wrk_num): | |
context = zmq.Context() | |
work_receiver = context.socket(zmq.PULL) | |
work_receiver.connect("tcp://127.0.0.1:5557") | |
# Wait for messages | |
while True: | |
work_message = work_receiver.recv_json() | |
product = work_message['num'] * work_message['num'] | |
answer_message = { 'worker' : wrk_num, 'result' : product } | |
print(answer_message) | |
if __name__ == "__main__": | |
# Create a pool of workers to distribute work to | |
worker_pool = range(3) | |
for wrk_num in range(len(worker_pool)): | |
print('starting worker') | |
Process(target=worker, args=(wrk_num,)).start() | |
# Start the ventilator! | |
ventilator = Process(target=ventilator, args=()) | |
ventilator.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment