{"id":174811,"date":"2024-11-07T23:57:22","date_gmt":"2024-11-07T15:57:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/174811\/"},"modified":"2024-11-07T23:57:23","modified_gmt":"2024-11-07T15:57:23","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%8f%90%e5%8d%87%e5%af%ab%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%88%e7%8e%87-%e5%a4%9a%e7%b7%9a%e7%a8%8b-%e5%af%ab%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/174811\/","title":{"rendered":"\u5982\u4f55\u5229\u7528\u591a\u7dda\u7a0b\u63d0\u5347\u5beb\u6578\u64da\u5eab\u6548\u7387 (\u591a\u7dda\u7a0b \u5beb\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%8f%90%e5%8d%87%e5%af%ab%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%88%e7%8e%87-gVoECfYpTv\">\u5982\u4f55\u5229\u7528\u591a\u7dda\u7a0b\u63d0\u5347\u5beb\u6578\u64da\u5eab\u6548\u7387<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6574\u9ad4\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u55ae\u7dda\u7a0b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u5f80\u5f80\u7121\u6cd5\u6eff\u8db3\u9ad8\u6548\u80fd\u7684\u8981\u6c42\u3002\u591a\u7dda\u7a0b\u6280\u8853\u7684\u5f15\u5165\uff0c\u70ba\u6578\u64da\u5eab\u7684\u5beb\u5165\u64cd\u4f5c\u63d0\u4f9b\u4e86\u65b0\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5229\u7528\u591a\u7dda\u7a0b\u63d0\u5347\u5beb\u6578\u64da\u5eab\u7684\u6548\u7387\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u5efa\u8b70\u3002<\/p>\n<h2 id=\"%e5%a4%9a%e7%b7%9a%e7%a8%8b%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-gVoECfYpTv\">\u591a\u7dda\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u591a\u7dda\u7a0b\u662f\u6307\u5728\u540c\u4e00\u9032\u7a0b\u4e2d\u540c\u6642\u904b\u884c\u591a\u500b\u7dda\u7a0b\u7684\u6280\u8853\u3002\u6bcf\u500b\u7dda\u7a0b\u53ef\u4ee5\u7368\u7acb\u57f7\u884c\u4efb\u52d9\uff0c\u9019\u6a23\u53ef\u4ee5\u5145\u5206\u5229\u7528\u591a\u6838\u8655\u7406\u5668\u7684\u8a08\u7b97\u80fd\u529b\u3002\u5c0d\u65bc\u6578\u64da\u5eab\u64cd\u4f5c\u4f86\u8aaa\uff0c\u591a\u7dda\u7a0b\u53ef\u4ee5\u540c\u6642\u8655\u7406\u591a\u500b\u5beb\u5165\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u7684\u5beb\u5165\u6548\u7387\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87%e5%a4%9a%e7%b7%9a%e7%a8%8b%e5%af%ab%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-gVoECfYpTv\">\u70ba\u4ec0\u9ebc\u9078\u64c7\u591a\u7dda\u7a0b\u5beb\u5165\u6578\u64da\u5eab<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u541e\u5410\u91cf\uff1a<\/strong>\u591a\u7dda\u7a0b\u53ef\u4ee5\u540c\u6642\u8655\u7406\u591a\u500b\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u541e\u5410\u91cf\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u5ef6\u9072\uff1a<\/strong>\u901a\u904e\u4e26\u884c\u8655\u7406\u8acb\u6c42\uff0c\u53ef\u4ee5\u6e1b\u5c11\u6bcf\u500b\u8acb\u6c42\u7684\u7b49\u5f85\u6642\u9593\uff0c\u5f9e\u800c\u964d\u4f4e\u6574\u9ad4\u5ef6\u9072\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u5229\u7528\u7387\uff1a<\/strong>\u591a\u7dda\u7a0b\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u5229\u7528\u7cfb\u7d71\u8cc7\u6e90\uff0c\u7279\u5225\u662f\u5728\u591a\u6838\u8655\u7406\u5668\u4e0a\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a4%9a%e7%b7%9a%e7%a8%8b%e5%af%ab%e5%85%a5%e7%9a%84%e6%8c%91%e6%88%b0-gVoECfYpTv\">\u591a\u7dda\u7a0b\u5beb\u5165\u7684\u6311\u6230<\/h2>\n<p>\u5118\u7ba1\u591a\u7dda\u7a0b\u5beb\u5165\u6578\u64da\u5eab\u6709\u8a31\u591a\u512a\u52e2\uff0c\u4f46\u4e5f\u9762\u81e8\u4e00\u4e9b\u6311\u6230\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u4e00\u81f4\u6027\uff1a<\/strong>\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\uff0c\u6578\u64da\u7684\u4e00\u81f4\u6027\u53ef\u80fd\u6703\u53d7\u5230\u5f71\u97ff\u3002\u9700\u8981\u4f7f\u7528\u9396\u6216\u5176\u4ed6\u540c\u6b65\u6a5f\u5236\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u6b63\u78ba\u6027\u3002<\/li>\n<li><strong>\u6b7b\u9396\u554f\u984c\uff1a<\/strong>\u591a\u7dda\u7a0b\u64cd\u4f5c\u53ef\u80fd\u5c0e\u81f4\u6b7b\u9396\uff0c\u9019\u6703\u5f71\u97ff\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7af6\u722d\uff1a<\/strong>\u591a\u500b\u7dda\u7a0b\u540c\u6642\u8a2a\u554f\u6578\u64da\u5eab\u8cc7\u6e90\u53ef\u80fd\u5c0e\u81f4\u7af6\u722d\uff0c\u5f9e\u800c\u5f71\u97ff\u6027\u80fd\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e7%8f%be%e5%a4%9a%e7%b7%9a%e7%a8%8b%e5%af%ab%e5%85%a5%e7%9a%84%e7%ad%96%e7%95%a5-gVoECfYpTv\">\u5be6\u73fe\u591a\u7dda\u7a0b\u5beb\u5165\u7684\u7b56\u7565<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5be6\u73fe\u591a\u7dda\u7a0b\u5beb\u5165\u6578\u64da\u5eab\u7684\u7b56\u7565\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8%e9%80%a3%e6%8e%a5%e6%b1%a0-gVoECfYpTv\">1. \u4f7f\u7528\u9023\u63a5\u6c60<\/h3>\n<p>\u9023\u63a5\u6c60\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\uff0c\u6e1b\u5c11\u6bcf\u6b21\u8acb\u6c42\u5efa\u7acb\u9023\u63a5\u7684\u958b\u92b7\u3002\u901a\u904e\u4f7f\u7528\u9023\u63a5\u6c60\uff0c\u53ef\u4ee5\u8b93\u591a\u500b\u7dda\u7a0b\u5171\u4eab\u6578\u64da\u5eab\u9023\u63a5\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<pre><code>import threading\nimport queue\nimport time\nimport random\n\ndef db_write(data):\n    # \u6a21\u64ec\u6578\u64da\u5eab\u5beb\u5165\u64cd\u4f5c\n    time.sleep(random.uniform(0.1, 0.5))\n    print(f\"\u5beb\u5165\u6578\u64da: {data}\")\n\ndef worker(q):\n    while not q.empty():\n        data = q.get()\n        db_write(data)\n        q.task_done()\n\ndata_queue = queue.Queue()\nfor i in range(100):\n    data_queue.put(f\"\u6578\u64da-{i}\")\n\nthreads = []\nfor i in range(10):\n    t = threading.Thread(target=worker, args=(data_queue,))\n    t.start()\n    threads.append(t)\n\nfor t in threads:\n    t.join()\n<\/code><\/pre>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8%e6%89%b9%e9%87%8f%e5%af%ab%e5%85%a5-gVoECfYpTv\">2. \u4f7f\u7528\u6279\u91cf\u5beb\u5165<\/h3>\n<p>\u5c07\u591a\u500b\u5beb\u5165\u8acb\u6c42\u5408\u4f75\u70ba\u4e00\u500b\u6279\u91cf\u8acb\u6c42\uff0c\u53ef\u4ee5\u6e1b\u5c11\u6578\u64da\u5eab\u7684\u4ea4\u4e92\u6b21\u6578\uff0c\u5f9e\u800c\u63d0\u9ad8\u6027\u80fd\u3002\u9019\u7a2e\u65b9\u6cd5\u7279\u5225\u9069\u5408\u65bc\u9700\u8981\u5beb\u5165\u5927\u91cf\u6578\u64da\u7684\u5834\u666f\u3002<\/p>\n<pre><code>def batch_write(data_list):\n    # \u6a21\u64ec\u6279\u91cf\u5beb\u5165\u64cd\u4f5c\n    time.sleep(0.5)\n    print(f\"\u6279\u91cf\u5beb\u5165\u6578\u64da: {data_list}\")\n\ndef worker(q):\n    while not q.empty():\n        data_batch = []\n        for _ in range(10):  # \u6bcf\u6b21\u8655\u740610\u689d\u6578\u64da\n            if not q.empty():\n                data_batch.append(q.get())\n        batch_write(data_batch)\n        for _ in data_batch:\n            q.task_done()\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-gVoECfYpTv\">\u7d50\u8ad6<\/h2>\n<p>\u591a\u7dda\u7a0b\u6280\u8853\u70ba\u63d0\u5347\u6578\u64da\u5eab\u5beb\u5165\u6548\u7387\u63d0\u4f9b\u4e86\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u53ef\u4ee5\u5145\u5206\u767c\u63ee\u591a\u6838\u8655\u7406\u5668\u7684\u512a\u52e2\uff0c\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u6027\u80fd\u3002\u7136\u800c\uff0c\u5728\u5be6\u65bd\u904e\u7a0b\u4e2d\uff0c\u958b\u767c\u8005\u9700\u8981\u6ce8\u610f\u6578\u64da\u4e00\u81f4\u6027\u3001\u6b7b\u9396\u548c\u8cc7\u6e90\u7af6\u722d\u7b49\u554f\u984c\u3002\u9078\u64c7\u5408\u9069\u7684\u7b56\u7565\uff0c\u5982\u4f7f\u7528\u9023\u63a5\u6c60\u548c\u6279\u91cf\u5beb\u5165\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u5347\u5beb\u5165\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9019\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u6574\u9ad4\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u5229\u7528\u591a\u7dda\u7a0b\u6280\u8853\u63d0\u5347\u6578\u64da\u5eab\u5beb\u5165\u6548\u7387\uff0c\u512a\u5316\u6027\u80fd\u4e26\u52a0\u901f\u6578\u64da\u8655\u7406\u904e\u7a0b\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-174811","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/174811","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=174811"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/174811\/revisions"}],"predecessor-version":[{"id":174812,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/174811\/revisions\/174812"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=174811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=174811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=174811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}