{"id":97004,"date":"2024-10-20T14:11:16","date_gmt":"2024-10-20T06:11:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/97004\/"},"modified":"2024-10-20T14:11:17","modified_gmt":"2024-10-20T06:11:17","slug":"python%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%ef%bc%8c%e6%8f%90%e9%ab%98%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab%e6%95%88%e7%8e%87-python","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/97004\/","title":{"rendered":"Python\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u9023\u63a5\uff0c\u63d0\u9ad8\u6578\u64da\u5eab\u8b80\u5beb\u6548\u7387 (python\u591a\u7dda\u7a0b\u5beb\u6578\u64da\u5eab\u9023\u63a5)"},"content":{"rendered":"<h1 id=\"python%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%ef%bc%8c%e6%8f%90%e9%ab%98%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%af%ab%e6%95%88%e7%8e%87-zhvdmAxFFb\">Python\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u9023\u63a5\uff0c\u63d0\u9ad8\u6578\u64da\u5eab\u8b80\u5beb\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\u6578\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u6210\u70ba\u4e86\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002Python\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u7de8\u7a0b\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7dda\u7a0b\u7684\u652f\u6301\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5728\u540c\u4e00\u6642\u9593\u5167\u8655\u7406\u591a\u500b\u6578\u64da\u5eab\u8acb\u6c42\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u8b80\u5beb\u6548\u7387\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-zhvdmAxFFb\">\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\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\u6027\u80fd\u3002Python\u7684\u6a19\u6e96\u5eab\u4e2d\u63d0\u4f9b\u4e86`threading`\u6a21\u584a\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u5275\u5efa\u548c\u7ba1\u7406\u7dda\u7a0b\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab-zhvdmAxFFb\">\u70ba\u4ec0\u9ebc\u4f7f\u7528\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u6548\u7387\uff1a<\/strong>\u591a\u7dda\u7a0b\u53ef\u4ee5\u540c\u6642\u8655\u7406\u591a\u500b\u8acb\u6c42\uff0c\u6e1b\u5c11\u7b49\u5f85\u6642\u9593\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u5229\u7528\uff1a<\/strong>\u5728I\/O\u5bc6\u96c6\u578b\u64cd\u4f5c\u4e2d\uff0c\u591a\u7dda\u7a0b\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u7cfb\u7d71\u8cc7\u6e90\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u591a\u7dda\u7a0b\u53ef\u4ee5\u4f7f\u4ee3\u78bc\u7d50\u69cb\u66f4\u6e05\u6670\uff0c\u6613\u65bc\u7dad\u8b77\u3002<\/li>\n<\/ul>\n<h2 id=\"python%e4%b8%ad%e5%a4%9a%e7%b7%9a%e7%a8%8b%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e5%af%a6%e7%8f%be-zhvdmAxFFb\">Python\u4e2d\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u7684\u5be6\u73fe<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u7684`threading`\u6a21\u584a\u4f86\u5be6\u73fe\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u9023\u63a5\u3002<\/p>\n<pre><code>import threading\nimport sqlite3\nimport time\n\n# \u6578\u64da\u5eab\u64cd\u4f5c\u51fd\u6578\ndef db_operation(thread_id):\n    conn = sqlite3.connect('example.db')\n    cursor = conn.cursor()\n    \n    # \u6a21\u64ec\u6578\u64da\u5eab\u5beb\u5165\u64cd\u4f5c\n    cursor.execute(\"INSERT INTO test_table (thread_id, timestamp) VALUES (?, ?)\", (thread_id, time.time()))\n    conn.commit()\n    conn.close()\n    print(f\"Thread {thread_id} finished writing to the database.\")\n\n# \u5275\u5efa\u591a\u500b\u7dda\u7a0b\nthreads = []\nfor i in range(5):\n    thread = threading.Thread(target=db_operation, args=(i,))\n    threads.append(thread)\n    thread.start()\n\n# \u7b49\u5f85\u6240\u6709\u7dda\u7a0b\u5b8c\u6210\nfor thread in threads:\n    thread.join()\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e94\u500b\u7dda\u7a0b\uff0c\u6bcf\u500b\u7dda\u7a0b\u90fd\u6703\u5411\u6578\u64da\u5eab\u4e2d\u5beb\u5165\u4e00\u689d\u8a18\u9304\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u5beb\u5165\u6548\u7387\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u60c5\u6cc1\u4e0b\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-zhvdmAxFFb\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u6642\uff0c\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805\u9700\u8981\u8003\u616e\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u5eab\u9023\u63a5\u6c60\uff1a<\/strong>\u70ba\u4e86\u907f\u514d\u6bcf\u500b\u7dda\u7a0b\u90fd\u5275\u5efa\u65b0\u7684\u6578\u64da\u5eab\u9023\u63a5\uff0c\u53ef\u4ee5\u4f7f\u7528\u9023\u63a5\u6c60\u4f86\u7ba1\u7406\u6578\u64da\u5eab\u9023\u63a5\u3002<\/li>\n<li><strong>\u9396\u6a5f\u5236\uff1a<\/strong>\u5728\u591a\u7dda\u7a0b\u74b0\u5883\u4e2d\uff0c\u5c0d\u5171\u4eab\u8cc7\u6e90\u7684\u8a2a\u554f\u9700\u8981\u4f7f\u7528\u9396\u4f86\u907f\u514d\u6578\u64da\u4e0d\u4e00\u81f4\u7684\u554f\u984c\u3002<\/li>\n<li><strong>\u6027\u80fd\u6e2c\u8a66\uff1a<\/strong>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u61c9\u9032\u884c\u6027\u80fd\u6e2c\u8a66\u4ee5\u78ba\u5b9a\u6700\u4f73\u7684\u7dda\u7a0b\u6578\u91cf\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-zhvdmAxFFb\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528Python\u7684\u591a\u7dda\u7a0b\u6280\u8853\u53ef\u4ee5\u6709\u6548\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u8b80\u5beb\u6548\u7387\uff0c\u7279\u5225\u662f\u5728\u9ad8\u4e26\u767c\u7684\u5834\u666f\u4e0b\u3002\u901a\u904e\u5408\u7406\u7684\u8a2d\u8a08\u548c\u5be6\u73fe\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5145\u5206\u5229\u7528\u7cfb\u7d71\u8cc7\u6e90\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\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>\u4f86\u90e8\u7f72\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u9019\u5c07\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u904b\u884c\u74b0\u5883\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Python\u591a\u7dda\u7a0b\u64cd\u4f5c\u6578\u64da\u5eab\u9023\u63a5\uff0c\u63d0\u5347\u6578\u64da\u5eab\u7684\u8b80\u5beb\u6548\u7387\uff0c\u5be6\u73fe\u9ad8\u6548\u6578\u64da\u8655\u7406\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-97004","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97004","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=97004"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97004\/revisions"}],"predecessor-version":[{"id":97005,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/97004\/revisions\/97005"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=97004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=97004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=97004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}