{"id":297002,"date":"2020-01-12T03:00:06","date_gmt":"2020-01-12T03:00:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=297002"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2020-01-12T03:00:06","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=297002","title":{"rendered":"\u041e\u0441\u043d\u043e\u0432\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 zmq \u0432 python, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e key\/value \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/483586\/\">\n<h4>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e key\/value \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u043e\u0433\u043e \u043a\u0430\u043a memcache. \u0423\u0441\u0442\u0440\u043e\u0435\u043d \u043e\u043d \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 hashmap. \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0438\u043c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 tcp-\u0441\u043e\u043a\u0435\u0442. \u0412 \u043f\u0438\u0442\u043e\u043d\u0435 hashmap \u2014 \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0439 dict. \u0414\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c zeromq.<br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430<\/h4>\n<p>  \u0414\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u044d\u0442\u043e\u0433\u043e \u043f\u0430\u043a\u0435\u0442\u0430 \u0432 debian\/ubuntu \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0432\u0435\u0441\u0442\u0438 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438<br \/>  <code>sudo apt-get install libzmq-dev<\/code><br \/>  <code>sudo pip install zmq<\/code><\/p>\n<h4>\u041a\u043e\u0434<\/h4>\n<p>  \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0430\u0448\u0438\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c:<br \/>  \u0422\u0438\u043f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e zmq-\u0441\u043e\u043a\u0435\u0442\u0430 \u2014 REQ(REQuest, \u0437\u0430\u043f\u0440\u043e\u0441), \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u2014 \u0436\u0434\u0435\u043c \u043e\u0442\u0432\u0435\u0442.<br \/>  \u0427\u0442\u043e\u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u043e \u0441\u0435\u0442\u0438 \u043b\u044e\u0431\u043e\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c pickle. \u00ab\u041f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u00bb \u0440\u0430\u0431\u043e\u0442\u044b \u2014 \u043a\u043e\u0440\u0442\u0435\u0436 \u0438\u0437 \u0442\u0440\u0435\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439: (\u043a\u043e\u043c\u0430\u043d\u0434\u0430, \u043a\u043b\u044e\u0447, \u0434\u0430\u043d\u043d\u044b\u0435)<\/p>\n<pre><code class=\"python\">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' <\/code><\/pre>\n<h5>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/h5>\n<p>  cache = SuperCacher()<br \/>  cache.set(&#8216;key&#8217;, &#8216;value&#8217;)<br \/>  cache.get(&#8216;key&#8217;)<\/p>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0434\u043e\u043c\u0430\u0448\u043d\u0435\u0433\u043e \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u2014 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0430\u0434\u0440\u0435\u0441\/\u043f\u043e\u0440\u0442 \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043a\u043b\u0430\u0441\u0441\u0430.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0441\u0430\u043c \u0441\u0435\u0440\u0432\u0435\u0440.<br \/>  \u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u043a\u0435\u0442 REP(REPly, \u043e\u0442\u0432\u0435\u0442) \u2014 \u0436\u0434\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441, \u0448\u043b\u0435\u043c \u043e\u0442\u0432\u0435\u0442. \u0420\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441, \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u043c \u043b\u0438\u0431\u043e &#8216;ok&#8217; \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u043b\u0438\u0431\u043e \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \/ None \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0447\u0442\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"python\">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() <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439<br \/>  <code>python daemon.py<\/code><\/p>\n<p>  \u0412 \u0441\u043e\u0441\u0435\u0434\u043d\u0435\u0439 \u0432\u043a\u043b\u0430\u0434\u043a\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c python \u0432 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435.<\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; from lib import SuperCacher &gt;&gt;&gt; cache=SuperCacher() &gt;&gt;&gt; cache.set('key', 'value') True &gt;&gt;&gt; cache.get('key') 'value' <\/code><\/pre>\n<p>  \u041e \u0447\u0443\u0434\u043e, \u043e\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442! \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u043c\u0435\u043b\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0441\u0432\u043e\u0435\u043c \u0440\u0435\u0437\u044e\u043c\u0435 \u00ab\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 key-value \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 zmq\u00bb<\/p><\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/483586\/\"> https:\/\/habr.com\/ru\/post\/483586\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/483586\/\">\n<h4>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h4>\n<p>  \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e key\/value \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a\u043e\u0433\u043e \u043a\u0430\u043a memcache. \u0423\u0441\u0442\u0440\u043e\u0435\u043d \u043e\u043d \u043f\u0440\u043e\u0441\u0442\u043e \u2014 \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 hashmap. \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u043d\u0438\u043c \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 tcp-\u0441\u043e\u043a\u0435\u0442. \u0412 \u043f\u0438\u0442\u043e\u043d\u0435 hashmap \u2014 \u044d\u0442\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0439 dict. \u0414\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c zeromq.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-297002","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297002","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297002"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/297002\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=297002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=297002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}