{"id":202328,"date":"2025-05-10T14:24:54","date_gmt":"2025-05-10T06:24:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202328\/"},"modified":"2025-05-10T14:24:54","modified_gmt":"2025-05-10T06:24:54","slug":"%e6%80%8e%e4%b9%88%e7%94%a8redispython%e5%81%9a%e6%b6%88%e6%81%af%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202328\/","title":{"rendered":"\u600e\u4e48\u7528redis+python\u505a\u6d88\u606f\u961f\u5217"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u600e\u4e48\u7528redis+python\u505a\u6d88\u606f\u961f\u5217<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u600e\u4e48\u7528redis+python\u505a\u6d88\u606f\u961f\u5217\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<h3>\u4e00\u3001\u4f7f\u7528redis\u7684List\u7c7b\u578b\u7ed3\u5408lpush \u548c brpop \u6765\u5b9e\u73b0<\/h3>\n<h4>\u7b80\u4ecb<\/h4>\n<ul>\n<li>\n<p>\u9996\u5148redis\u7684List \u76f8\u5f53\u4e8e\u4e00\u4e2a\u961f\u5217\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5148\u8fdb\u5148\u51fa\u7684\u89c4\u5219<\/p>\n<\/li>\n<li>\n<p>\u91c7\u7528brpop \u662f\u56e0\u4e3a\u5f53\u961f\u5217\u4e2d\u6ca1\u6709\u7684\u65f6\u5019\u4f1a\u8fdb\u884c\u963b\u585e\uff0c\u76f4\u5230\u961f\u5217\u4e2d\u6709\u53ef\u5f39\u51fa\u5143\u7d20\u6216\u8005\u7b49\u5f85\u8d85\u65f6<\/p>\n<\/li>\n<\/ul>\n<h4>\u6a21\u62df\u95ee\u9898\uff1a<\/h4>\n<ul>\n<li>\n<p>\u8bbf\u95ee\u592a\u591a\uff0c\u670d\u52a1\u5668\u5904\u7406\u901f\u5ea6\u592a\u6162\uff0c\u5982\u679c\u6bcf\u9694\u7528\u6237\u7b49\u5f85\uff0c\u670d\u52a1\u5668\u53cd\u9988\u7684\u8bdd\uff0c\u65f6\u95f4\u592a\u957f\uff0chttp\u8fde\u63a5\u8d85\u65f6\uff0c\u51fa\u73b0\u670d\u52a1\u5668\u9519\u8bef\u3002<\/p>\n<\/li>\n<\/ul>\n<h4>\u6a21\u62df\u5b9e\u73b0\u8fc7\u7a0b\uff1a<\/h4>\n<ul>\n<li>\n<p>\u6709\u4e00\u4e2a\u5ba2\u6237\u7aef\u4e0d\u65ad\u7684\u5f80\u961f\u5217\u91cc\u653e\u4e1c\u897f(\u6570\u636e)\uff0c\u91c7\u7528\u591a\u7ebf\u7a0b\uff0c\u6a21\u62df\u5927\u91cf\u7528\u6237\u8bbf\u95ee\u7684\u60c5\u51b5<\/p>\n<\/li>\n<li>\n<p>\u6709\u4e00\u4e2a\u670d\u52a1\u5668\u4e0d\u65ad\u7684\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u6253\u5370\uff0c\u5e76\u8bbe\u7f6e\u6bcf\u6b21\u6253\u5370\u65f6\u95f4\u77612\u79d2<\/p>\n<\/li>\n<\/ul>\n<h4>redis\u7684List\u7ed3\u6784\u4ecb\u7ecd<\/h4>\n<pre>key&nbsp;[value,&nbsp;value]\nkey&nbsp;\u4ee3\u8868List\u7684\u540d\u5b57\uff0c&nbsp;[value,&nbsp;...]&nbsp;\u662f\u503c<\/pre>\n<p>\u5ba2\u6237client.py<\/p>\n<pre>import&nbsp;random\nimport&nbsp;threading\nimport&nbsp;redis\nimport&nbsp;config\n\nlock&nbsp;=&nbsp;threading.Lock()\nlock.acquire()\nlock.release()\n\n\npool&nbsp;=&nbsp;redis.ConnectionPool(host=config.HOST,&nbsp;port=config.PORT,&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;decode_responses=True,&nbsp;password=config.PASSWORD)\n\nr&nbsp;=&nbsp;redis.Redis(connection_pool=pool)\n\n#&nbsp;\u5ba2\u6237\u5f80redis&nbsp;\u4e2d\u653e\u6570\u636e\ndef&nbsp;fun1(redisObj):\n&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;=&nbsp;random.randint(0,&nbsp;100)\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5f80ccc\u5217\u8868\u4e2d\u5b58\u653e\n&nbsp;&nbsp;&nbsp;&nbsp;print(\"\u5f00\u59cb\u53d1\u9001\u6570\u636e:\",&nbsp;value)\n&nbsp;&nbsp;&nbsp;&nbsp;redisObj.lpush(\"print\",str(value))\n\nfor&nbsp;i&nbsp;in&nbsp;range(100):\n&nbsp;&nbsp;&nbsp;&nbsp;threading.Thread(target=fun1,&nbsp;args=(r,)).start()<\/pre>\n<p>\u670d\u52a1\u5668server.py<\/p>\n<pre>import&nbsp;redis\nimport&nbsp;time\nimport&nbsp;config\n\n\npool&nbsp;=&nbsp;redis.ConnectionPool(host=config.HOST,&nbsp;port=config.PORT,&nbsp;decode_responses=True,&nbsp;password=config.PASSWORD)\nr&nbsp;=&nbsp;redis.Redis(connection_pool=pool)\n#&nbsp;\u670d\u52a1\u7aef\u4e0d\u65ad\u7684\u53d6\nwhile&nbsp;True:\n&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;=&nbsp;r.brpop(\"print\")\n&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(2)\n&nbsp;&nbsp;&nbsp;&nbsp;print(value)<\/pre>\n<h3>\u95ee\u9898\u56de\u987e<\/h3>\n<h4>\u6211\u4eec\u4e4b\u524d\u8bf4\u5b58\u5728\u963b\u585e\u592a\u4e45\u65ad\u5f00\u8fde\u63a5\u7684\u95ee\u9898\uff0c\u89e3\u51b3\u4e0b<\/h4>\n<p>\u65b9\u5f0f\uff1a \u5c06\u8fde\u63a5\u4f5c\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u8fdb\u884c\u9519\u8bef\u6355\u6349\uff0c\u53d1\u751f\u95ee\u9898\u7684\u65f6\u5019\u91cd\u65b0\u8fde\u63a5\u3002<\/p>\n<pre>import&nbsp;redis\nimport&nbsp;time\nimport&nbsp;config\n\ndef&nbsp;get_redis():\n&nbsp;&nbsp;&nbsp;&nbsp;pool&nbsp;=&nbsp;redis.ConnectionPool(host=config.HOST,&nbsp;port=config.PORT,&nbsp;decode_responses=True,&nbsp;password=config.PASSWORD)\n&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;redis.Redis(connection_pool=pool)\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;r\n#&nbsp;\u670d\u52a1\u7aef\u4e0d\u65ad\u7684\u53d6\nr&nbsp;=&nbsp;get_redis()\n&nbsp;&nbsp;&nbsp;&nbsp;\nwhile&nbsp;True:\n&nbsp;&nbsp;&nbsp;&nbsp;try:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value&nbsp;=&nbsp;r.brpop(\"print\")\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time.sleep(2)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(value)\n&nbsp;&nbsp;&nbsp;&nbsp;except&nbsp;Exception&nbsp;as&nbsp;e:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(\"\u7b49\u5f85\u8d85\u65f6\u91cd\u8fde\")\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r&nbsp;=&nbsp;get_redis()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u600e\u4e48\u7528redis+python\u505a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202328","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202328","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202328"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202328\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}