{"id":5116,"date":"2024-11-14T12:50:21","date_gmt":"2024-11-14T04:50:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5116\/"},"modified":"2024-11-14T12:50:21","modified_gmt":"2024-11-14T04:50:21","slug":"redis%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5116\/","title":{"rendered":"redis\u591a\u7ebf\u7a0b\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  redis \u652f\u6301\u591a\u7ebf\u7a0b\u64cd\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u5e76\u53d1\u6027\u548c\u541e\u5410\u91cf\u3002\u4f7f\u7528pubsub\u673a\u5236\uff0c\u53ef\u4ee5\u5728\u7ebf\u7a0b\u4e4b\u95f4\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\uff1b\u901a\u8fc7\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u9ad8\u6548\u5206\u914d\u548c\u7ba1\u7406\u9884\u5148\u521b\u5efa\u7684\u7ebf\u7a0b\u3002\u6ce8\u610f\u4e8b\u9879\u5305\u62ec\u7ebf\u7a0b\u5b89\u5168\u3001\u4f7f\u7528\u76f8\u540credis\u5b9e\u4f8b\u548c\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u7ebf\u7a0b\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/21\/2024052117302570037.jpg\" class=\"aligncenter\" title=\"redis\u591a\u7ebf\u7a0b\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"redis\u591a\u7ebf\u7a0b\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>Redis \u591a\u7ebf\u7a0b\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<p>Redis \u652f\u6301\u591a\u7ebf\u7a0b\u64cd\u4f5c\uff0c\u5141\u8bb8\u5728\u5355\u4e2a Redis \u5b9e\u4f8b\u4e2d\u540c\u65f6\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\u6216\u4efb\u52a1\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u541e\u5410\u91cf\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<p>\u4f7f\u7528 Redis \u591a\u7ebf\u7a0b\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li> <strong>pubsub<\/strong>\uff1a\u53d1\u5e03\/\u8ba2\u9605\u673a\u5236\uff0c\u5141\u8bb8\u7ebf\u7a0b\u4e4b\u95f4\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u3002<\/li>\n<li> <strong>\u7ebf\u7a0b\u6c60<\/strong>\uff1a\u9884\u5148\u521b\u5efa\u7684\u7ebf\u7a0b\u96c6\u5408\uff0c\u7528\u4e8e\u6267\u884c\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><strong>pubsub<\/strong><\/p>\n<p>pubsub \u673a\u5236\u4f7f\u7528 SUBSCRIBE \u548c PUBLISH \u547d\u4ee4\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e4b\u95f4\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre># \u521b\u5efa\u8ba2\u9605\u7ebf\u7a0b\nimport threading\nimport redis\n\ndef subscribe_thread():\n    r = redis.Redis()\n    pubsub = r.pubsub()\n    pubsub.subscribe('my-channel')\n    for message in pubsub.listen():\n        print(message)\n\n# \u521b\u5efa\u53d1\u5e03\u7ebf\u7a0b\ndef publish_thread():\n    r = redis.Redis()\n    r.publish('my-channel', 'Hello world!')\n\n# \u542f\u52a8\u7ebf\u7a0b\nsubscribe_thread = threading.Thread(target=subscribe_thread)\npublish_thread = threading.Thread(target=publish_thread)\nsubscribe_thread.start()\npublish_thread.start()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ebf\u7a0b\u6c60<\/strong><\/p>\n<p>\u7ebf\u7a0b\u6c60\u901a\u8fc7\u521b\u5efa\u9884\u5148\u521b\u5efa\u7684\u7ebf\u7a0b\u96c6\u5408\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u5b83\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u5728\u4e0d\u521b\u5efa\u65b0\u7ebf\u7a0b\u7684\u60c5\u51b5\u4e0b\u5206\u914d\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002<\/p>\n<pre>from concurrent.futures import ThreadPoolExecutor\n\ndef task(arg):\n    print(f'Task {arg} executed')\n\n# \u521b\u5efa\u7ebf\u7a0b\u6c60\nwith ThreadPoolExecutor(max_workers=4) as executor:\n    # \u63d0\u4ea4\u4efb\u52a1\n    for i in range(10):\n        executor.submit(task, i)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u4f7f\u7528 Redis \u591a\u7ebf\u7a0b\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u4e8b\u9879\uff1a<\/p>\n<ul>\n<li>Redis \u4e2d\u7684\u6240\u6709\u6570\u636e\u7ed3\u6784\u90fd\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/li>\n<li>\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u4f7f\u7528\u76f8\u540c\u7684 Redis \u5b9e\u4f8b\u3002<\/li>\n<li>\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u7ebf\u7a0b\uff0c\u56e0\u4e3a\u8fc7\u591a\u7684\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u591a\u7ebf\u7a0b\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>redis \u652f\u6301\u591a\u7ebf\u7a0b\u64cd\u4f5c\uff0c\u4ee5\u63d0\u9ad8\u5e76\u53d1\u6027\u548c\u541e\u5410\u91cf\u3002\u4f7f\u7528pubsub\u673a\u5236\uff0c\u53ef\u4ee5\u5728\u7ebf\u7a0b\u4e4b\u95f4\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\uff1b\u901a\u8fc7\u7ebf\u7a0b\u6c60\uff0c\u53ef\u4ee5\u9ad8\u6548\u5206\u914d\u548c\u7ba1\u7406\u9884\u5148\u521b\u5efa\u7684\u7ebf\u7a0b\u3002\u6ce8\u610f\u4e8b\u9879\u5305\u62ec\u7ebf\u7a0b\u5b89\u5168\u3001\u4f7f\u7528\u76f8\u540credis\u5b9e\u4f8b\u548c\u907f\u514d\u8fc7\u5ea6\u4f7f\u7528\u7ebf\u7a0b\u3002 Redis \u591a\u7ebf\u7a0b\u4f7f\u7528\u65b9\u6cd5 Redis \u652f\u6301\u591a\u7ebf\u7a0b\u64cd\u4f5c\uff0c\u5141\u8bb8\u5728\u5355\u4e2a Redis \u5b9e\u4f8b\u4e2d\u540c\u65f6\u6267\u884c\u591a\u4e2a\u64cd\u4f5c\u6216\u4efb\u52a1\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u5e76\u53d1\u6027\u548c\u541e\u5410\u91cf\u3002 \u4f7f\u7528\u65b9\u6cd5 \u4f7f\u7528 Redis \u591a\u7ebf\u7a0b\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a pubsub\uff1a\u53d1\u5e03\/\u8ba2\u9605\u673a\u5236\uff0c\u5141\u8bb8\u7ebf\u7a0b\u4e4b\u95f4\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u3002 \u7ebf\u7a0b\u6c60\uff1a\u9884\u5148\u521b\u5efa\u7684\u7ebf\u7a0b\u96c6\u5408\uff0c\u7528\u4e8e\u6267\u884c\u4efb\u52a1\u3002 pubsub pubsub \u673a\u5236\u4f7f\u7528 SUBSCRIBE \u548c PUBLISH \u547d\u4ee4\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e4b\u95f4\u53d1\u9001\u548c\u63a5\u6536\u6d88\u606f\u3002\u4f8b\u5982\uff1a # \u521b\u5efa\u8ba2\u9605\u7ebf\u7a0b import threading import redis def subscribe_thread(): r = redis.Redis() pubsub = r.pubsub() pubsub.subscribe(&#8216;my-channel&#8217;) for message in pubsub.listen(): print(message) # \u521b\u5efa\u53d1\u5e03\u7ebf\u7a0b def publish_thread(): r = redis.Redis() r.publish(&#8216;my-channel&#8217;, &#8216;Hello world!&#8217;) # \u542f\u52a8\u7ebf\u7a0b subscribe_thread = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5116","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5116"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5116\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}