Lub hauv paus ntawm kev ua haujlwm nrog zmq hauv python, tsim kom muaj qhov yuam sij yooj yim / khoom muaj nqis

Taw qhia

Cia peb saib ib qho piv txwv ntawm qhov yooj yim qhov tseem ceeb / tus nqi cia, xws li memcache. Nws yog tsim los yooj yim - cov ntaub ntawv khaws cia hauv lub cim xeeb, hauv cov qauv hashmap. Lawv nkag los ntawm lub qhov (socket) tcp. Hauv Python, hashmap yog cov lus hais tsis tu ncua. Rau kev nkag tau peb yuav siv zeromq.

hloov

Txhawm rau nruab cov pob no hauv debian / ubuntu, tsuas yog nkag rau hauv console
sudo apt-get install libzmq-dev
sudo pip install zmq

code

Cia peb sau ib chav kawm ua haujlwm nrog peb cov server:
Hom zmq qhov (socket) siv yog REQ (REQuest, thov), peb xa ib daim ntawv thov thiab tos kom teb.
Txhawm rau khaws thiab xa txhua hom ntaub ntawv hla lub network, peb siv tus qauv pickle module. Txoj haujlwm "txheej txheem" ​​yog tuple ntawm peb qhov tseem ceeb: (cov lus txib, qhov tseem ceeb, cov ntaub ntawv)

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'
Siv

cache = SuperCacher()
cache.set('key', 'tus nqi')
cache.get('key')

Raws li kev ua homework, txhim kho qhov kev siv los ntawm kev ntxiv peev xwm los qhia qhov chaw nyob / chaw nres nkoj thaum tsim ib qho piv txwv ntawm chav kawm.

Tam sim no cia peb sau lub server nws tus kheej.
Lub sijhawm no siv lub qhov (socket) REP (REPly, teb) - peb tab tom tos qhov kev thov, xa lus teb. Peb txheeb xyuas qhov kev thov thiab teb nrog 'ok' thaum sau ntawv, lossis nrog cov ntaub ntawv / Tsis muaj nyob rau hauv cov ntaub ntawv nyeem.

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()

Txhawm rau kuaj txhua yam ua ke, peb pib lub server nrog cov lus txib
python daemon.py

Hauv tab tom ntej, tso python hauv kev sib tham sib hom.

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

Huag txuj ci tseem ceeb, nws ua haujlwm! Tam sim no koj tuaj yeem ruaj ntseg sau rau hauv koj qhov kev xav tau "kev txhim kho ntawm qhov tseem ceeb-nqi khaws cia siv zmq raws tu qauv"

Tau qhov twg los: www.hab.com

Ntxiv ib saib