Basics of working with zmq in python, creating a simple key/value store

Introduction

Let's look at an example of a simple key / value storage, such as memcache. It is arranged simply - the data is stored in memory, in the hashmap structure. Access to them is carried out through a tcp socket. In python, hashmap is just a dict. For access we will use zeromq.

Setting

To install this package in debian/ubuntu, just enter in the console
sudo apt-get install libzmq-dev
sudo pip install zmq

Code

Let's write a class to work with our server:
The type of zmq socket used is REQ(REQuest, request), we send a request - we are waiting for a response.
To store and transmit any type of data over the network, we use the standard pickle module. "Protocol" of work - a tuple of three values: (command, key, data)

import zmq
import pickle

class SuperCacher:
    def __init__(self):
        context = zmq.Context()
        self.socket = context.socket(zmq.REQ)
        self.socket.connect('tcp://127.0.0.1:43000')

    def get(self, key):
        self.socket.send(pickle.dumps(('get', key, None)))
        return pickle.loads(self.socket.recv())

    def set(self, key, data):
        self.socket.send(pickle.dumps(('set', key, data)))
        return self.socket.recv() == b'ok'
Using

cache = SuperCacher()
cache.set('key', 'value')
cache.get('key')

As a homework, improve the implementation by adding the ability to specify an address/port when creating an instance of the class.

Now let's write the server itself.
This time, the REP(REPly, response) socket is used - we are waiting for the request, we send the answer. We parse the request, answer either 'ok' in case of writing, or data / None in case of reading.

import pickle
import json
import zmq

def run_daemon():
    memory = {}

    context = zmq.Context()
    socket = context.socket(zmq.REP)
    socket.bind('tcp://127.0.0.1:43000')

    while True:
        try:
            command, key, data = pickle.loads(socket.recv())
            if command == 'set':
                memory[key] = data
                socket.send(b'ok')
            elif command == 'get':
                result = memory.get(key, None)
                socket.send(pickle.dumps(result))
        except Exception as e:
            print(e)

if __name__ == '__main__':
    run_daemon()

To test everything together, start the server with the command
python daemon.py

In the next tab, run python in interactive mode.

>>> from lib import SuperCacher
>>> cache=SuperCacher()
>>> cache.set('key', 'value')
True
>>> cache.get('key')
'value'

Wow, it works! Now you can safely write in your resume "development of key-value storage using the zmq protocol"

Source: habr.com

Add a comment