Skip to content

Instantly share code, notes, and snippets.

@wisehackermonkey
Last active July 30, 2020 18:51
Show Gist options
  • Save wisehackermonkey/d0aaf4f808b91e1d0d1f745770d08c41 to your computer and use it in GitHub Desktop.
Save wisehackermonkey/d0aaf4f808b91e1d0d1f745770d08c41 to your computer and use it in GitHub Desktop.
Research into what zmq is

Zmq

ZeroMQ

looks like its similar to mqtt but faster and more flexiable written in c/c++, and supports bindings in python (most docs are in c++) does not require a server

Frames

  • frames are (message parts) blocks of data with specific length. has garente of being recieved at the other end
  • to send large files spliting up into parts.

Sending strings

  • sent with out a trialing NULL 'asdfasdf\0' is 'asdfa\0'
    • have specified length

Normal Sockets vs zmq sockets

  • normal: client and server make connection

  • zmq: each connection to socket creates its own que, allowing for 1-many, many-1, many-many

    • ROUTER: is the acception, only create que if peer has acknowleged the connection, AKA confirm before setup
  • Ha funny: form a karmic circle of socket life ZeroMQ | Socket API

import zmq

context = zmq.Context()

#  Socket to talk to server
print("Connecting to hello world server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

#  Do 10 requests, waiting each time for a response
for request in range(10):
    print("Sending request %s …" % request)
    socket.send(b"Hello")

    #  Get the reply.
    message = socket.recv()
    print("Received reply %s [ %s ]" % (request, message))
t time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next request from client
    message = socket.recv()
    print("Received request: %s" % message)

    #  Do some 'work'
    time.sleep(1)

    #  Send reply back to client
    socket.send(b"World")
  • Because zmq doesnt require trailing null in string being sent
    • can cause weird results. rule of thumb copy string a terminate with \0 every time

ZeroMQ book Notes

One-way data distrobution

  • weather server zmq.PUB
import zmq
from random import randrange

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")

while True:
    zipcode = randrange(1, 100000)
    temperature = randrange(-80, 135)
    relhumidity = randrange(10, 60)

    socket.send_string("%i %i %i" % (zipcode, temperature, relhumidity))
  • weather client zmq.SUB
import sys
import zmq

#  Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

print("Collecting updates from weather server…")
socket.connect("tcp://localhost:5556")

# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"

# Python 2 - ascii bytes to unicode str
if isinstance(zip_filter, bytes):
    zip_filter = zip_filter.decode('ascii')
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)

# Process 5 updates
total_temp = 0
for update_nbr in range(5):
    string = socket.recv_string()
    zipcode, temperature, relhumidity = string.split()
    total_temp += int(temperature)

print("Average temperature for zipcode '%s' was %dF" % (
      zip_filter, total_temp / (update_nbr+1))
)

Pub sub

issue publisher start up is and subscirber doesnt get data on first try

Divide and conquer

Chapter One - ØMQ - The Guide


Resources:


Questions:

  • zmq vs mqtt
  • how fast is zeromq
  • what is its common usecases
  • if it has no configuration then what does it do?
  • does it allow for functions to be called on server with request?
  • what is RPC?
  • what is #f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment