{"id":2875,"date":"2025-03-16T12:46:25","date_gmt":"2025-03-16T04:46:25","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2875"},"modified":"2025-03-16T12:46:25","modified_gmt":"2025-03-16T04:46:25","slug":"python3-xml-%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/16\/python3-xml-%e8%a7%a3%e6%9e%90\/","title":{"rendered":"Python3 XML \u89e3\u6790"},"content":{"rendered":"\n<p>\u5728 Python3 \u4e2d\uff0c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u7528\u6765\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u5c24\u5176\u662f I\/O \u5bc6\u96c6\u578b\u4efb\u52a1\u3002\u591a\u7ebf\u7a0b\u901a\u8fc7\u5e76\u53d1\u6267\u884c\u4efb\u52a1\u6765\u63d0\u5347\u7a0b\u5e8f\u6027\u80fd\uff0c\u4f46\u662f\u5728 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0c\u7531\u4e8e Python \u7684\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\uff0c\u7ebf\u7a0b\u53ef\u80fd\u65e0\u6cd5\u771f\u6b63\u5e76\u884c\u6267\u884c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u89e3\u6790 Python3 \u4e2d\u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5<\/strong><\/h3>\n\n\n\n<p>\u7ebf\u7a0b\u662f\u7a0b\u5e8f\u4e2d\u6267\u884c\u6d41\u7684\u6700\u5c0f\u5355\u4f4d\u3002\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u6709\u591a\u4e2a\u7ebf\u7a0b\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u5982\u5185\u5b58\u3001\u6587\u4ef6\u53e5\u67c4\u7b49\u3002\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5229\u7528\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>Python \u63d0\u4f9b\u4e86 <code>threading<\/code> \u6a21\u5757\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Python3 <code>threading<\/code> \u6a21\u5757<\/strong><\/h3>\n\n\n\n<p><code>threading<\/code> \u6a21\u5757\u662f Python \u63d0\u4f9b\u7684\u6807\u51c6\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\u3002\u8be5\u6a21\u5757\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u591a\u4e2a\u7ebf\u7a0b\u5e76\u63a7\u5236\u5b83\u4eec\u7684\u884c\u4e3a\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.1 \u521b\u5efa\u7ebf\u7a0b<\/strong><\/h4>\n\n\n\n<p>\u521b\u5efa\u7ebf\u7a0b\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u901a\u8fc7\u7ee7\u627f <code>threading.Thread<\/code> \u7c7b\uff0c\u6216\u8005\u901a\u8fc7\u76f4\u63a5\u521b\u5efa <code>Thread<\/code> \u7c7b\u5b9e\u4f8b\u5e76\u4f20\u9012\u76ee\u6807\u51fd\u6570\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 1\uff1a\u7ee7\u627f <code>Thread<\/code> \u7c7b<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\nimport time\n\n# \u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b\u7c7b\uff0c\u7ee7\u627f\u81ea Thread \u7c7b\nclass MyThread(threading.Thread):\n    def run(self):\n        print(f\"Thread {self.name} is starting.\")\n        time.sleep(2)\n        print(f\"Thread {self.name} has finished.\")\n\n# \u521b\u5efa\u7ebf\u7a0b\u5b9e\u4f8b\nthread1 = MyThread()\nthread2 = MyThread()\n\n# \u542f\u52a8\u7ebf\u7a0b\nthread1.start()\nthread2.start()\n\n# \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\nthread1.join()\nthread2.join()\n\nprint(\"Main thread finished.\")<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u65b9\u6cd5 2\uff1a\u76f4\u63a5\u521b\u5efa <code>Thread<\/code> \u5b9e\u4f8b<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\nimport time\n\n# \u76ee\u6807\u51fd\u6570\ndef worker():\n    print(f\"Thread {threading.current_thread().name} is starting.\")\n    time.sleep(2)\n    print(f\"Thread {threading.current_thread().name} has finished.\")\n\n# \u521b\u5efa\u7ebf\u7a0b\u5b9e\u4f8b\nthread1 = threading.Thread(target=worker)\nthread2 = threading.Thread(target=worker)\n\n# \u542f\u52a8\u7ebf\u7a0b\nthread1.start()\nthread2.start()\n\n# \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\nthread1.join()\nthread2.join()\n\nprint(\"Main thread finished.\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2 \u7ebf\u7a0b\u7684\u542f\u52a8\u4e0e\u6267\u884c<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>start()<\/code><\/strong>: \u542f\u52a8\u7ebf\u7a0b\uff0c\u7ebf\u7a0b\u4f1a\u81ea\u52a8\u8c03\u7528 <code>run()<\/code> \u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong><code>join()<\/code><\/strong>: \u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\u518d\u7ee7\u7eed\u6267\u884c\u3002\u8c03\u7528 <code>join()<\/code> \u540e\uff0c\u4e3b\u7ebf\u7a0b\u4f1a\u963b\u585e\uff0c\u76f4\u5230\u5b50\u7ebf\u7a0b\u6267\u884c\u5b8c\u6210\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.3 \u7ebf\u7a0b\u7684\u63a7\u5236<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>setDaemon(True)<\/code><\/strong>: \u5c06\u7ebf\u7a0b\u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u3002\u5b88\u62a4\u7ebf\u7a0b\u4f1a\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u81ea\u52a8\u9000\u51fa\u3002\u5982\u679c\u7ebf\u7a0b\u662f\u5b88\u62a4\u7ebf\u7a0b\uff0c\u5b83\u4e0d\u9700\u8981\u8c03\u7528 <code>join()<\/code> \u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong><code>is_alive()<\/code><\/strong>: \u68c0\u67e5\u7ebf\u7a0b\u662f\u5426\u4ecd\u7136\u5b58\u6d3b\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\nimport time\n\ndef worker():\n    print(\"Worker thread is running.\")\n    time.sleep(2)\n    print(\"Worker thread finished.\")\n\n# \u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b\nthread = threading.Thread(target=worker)\nthread.setDaemon(True)  # \u8bbe\u7f6e\u4e3a\u5b88\u62a4\u7ebf\u7a0b\nthread.start()\n\n# \u68c0\u67e5\u7ebf\u7a0b\u662f\u5426\u5b58\u6d3b\nprint(f\"Is thread alive? {thread.is_alive()}\")\n\ntime.sleep(1)\nprint(f\"Is thread alive after 1 second? {thread.is_alive()}\")\ntime.sleep(2)\nprint(f\"Is thread alive after 3 seconds? {thread.is_alive()}\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.4 \u7ebf\u7a0b\u95f4\u901a\u4fe1<\/strong><\/h4>\n\n\n\n<p>\u7ebf\u7a0b\u4e4b\u95f4\u53ef\u4ee5\u901a\u8fc7\u5171\u4eab\u53d8\u91cf\u3001\u961f\u5217\u7b49\u65b9\u5f0f\u8fdb\u884c\u901a\u4fe1\u3002\u5728 Python \u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f <code>queue.Queue<\/code> \u7c7b\uff0c\u5b83\u652f\u6301\u7ebf\u7a0b\u95f4\u7684\u5b89\u5168\u6570\u636e\u4f20\u8f93\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u961f\u5217\u4f20\u9012\u6570\u636e<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\nimport queue\n\n# \u521b\u5efa\u961f\u5217\nq = queue.Queue()\n\n# \u751f\u4ea7\u8005\u7ebf\u7a0b\ndef producer():\n    for i in range(5):\n        print(f\"Producing item {i}\")\n        q.put(i)\n\n# \u6d88\u8d39\u8005\u7ebf\u7a0b\ndef consumer():\n    while True:\n        item = q.get()  # \u83b7\u53d6\u961f\u5217\u4e2d\u7684\u9879\n        if item is None:  # \u901a\u8fc7 None \u505c\u6b62\u7ebf\u7a0b\n            break\n        print(f\"Consuming item {item}\")\n\n# \u521b\u5efa\u7ebf\u7a0b\nproducer_thread = threading.Thread(target=producer)\nconsumer_thread = threading.Thread(target=consumer)\n\n# \u542f\u52a8\u7ebf\u7a0b\nproducer_thread.start()\nconsumer_thread.start()\n\n# \u7b49\u5f85\u751f\u4ea7\u8005\u7ebf\u7a0b\u5b8c\u6210\nproducer_thread.join()\n\n# \u901a\u8fc7\u5411\u961f\u5217\u4e2d\u653e\u5165 None \u6765\u505c\u6b62\u6d88\u8d39\u8005\u7ebf\u7a0b\nq.put(None)\n\n# \u7b49\u5f85\u6d88\u8d39\u8005\u7ebf\u7a0b\u5b8c\u6210\nconsumer_thread.join()\n\nprint(\"All threads finished.\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.5 \u4f7f\u7528 <code>Lock<\/code> \u548c <code>RLock<\/code> \u89e3\u51b3\u7ebf\u7a0b\u540c\u6b65\u95ee\u9898<\/strong><\/h4>\n\n\n\n<p>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u7ade\u6001\u6761\u4ef6\uff08race condition\uff09\u3002\u4e3a\u4e86\u907f\u514d\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u5982 <code>Lock<\/code> \u548c <code>RLock<\/code>\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Lock<\/code><\/strong>\uff1a\u786e\u4fdd\u5728\u4efb\u4f55\u65f6\u523b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u591f\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong><code>RLock<\/code><\/strong>\uff1a\u53ef\u91cd\u5165\u9501\uff0c\u5141\u8bb8\u540c\u4e00\u7ebf\u7a0b\u591a\u6b21\u83b7\u5f97\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>Lock<\/code> \u8fdb\u884c\u7ebf\u7a0b\u540c\u6b65<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\n\nlock = threading.Lock()\n\n# \u5171\u4eab\u8d44\u6e90\ncounter = 0\n\n# \u7ebf\u7a0b\u4efb\u52a1\ndef increment():\n    global counter\n    with lock:  # \u83b7\u53d6\u9501\n        for _ in range(100000):\n            counter += 1\n\n# \u521b\u5efa\u7ebf\u7a0b\nthread1 = threading.Thread(target=increment)\nthread2 = threading.Thread(target=increment)\n\n# \u542f\u52a8\u7ebf\u7a0b\nthread1.start()\nthread2.start()\n\n# \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\nthread1.join()\nthread2.join()\n\nprint(f\"Final counter value: {counter}\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.6 \u7ebf\u7a0b\u6c60\uff1a<code>concurrent.futures<\/code><\/strong><\/h4>\n\n\n\n<p>\u5bf9\u4e8e\u5927\u91cf\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u7ba1\u7406\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\uff08<code>ThreadPoolExecutor<\/code>\uff09\u53ef\u4ee5\u66f4\u65b9\u4fbf\u9ad8\u6548\u5730\u7ba1\u7406\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528\u7ebf\u7a0b\u6c60<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>from concurrent.futures import ThreadPoolExecutor\n\n# \u4efb\u52a1\u51fd\u6570\ndef task(n):\n    print(f\"Task {n} is being executed.\")\n\n# \u521b\u5efa\u7ebf\u7a0b\u6c60\nwith ThreadPoolExecutor(max_workers=5) as executor:\n    executor.map(task, range(10))  # \u5206\u914d\u4efb\u52a1<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>ThreadPoolExecutor<\/code> \u4f1a\u81ea\u52a8\u7ba1\u7406 5 \u4e2a\u5de5\u4f5c\u7ebf\u7a0b\uff0c\u5e76\u5c06\u4efb\u52a1\u5206\u914d\u7ed9\u8fd9\u4e9b\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. GIL\uff08\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff09\u4e0e\u591a\u7ebf\u7a0b\u7684\u5c40\u9650\u6027<\/strong><\/h3>\n\n\n\n<p>Python \u7684 GIL\uff08Global Interpreter Lock\uff09\u4f7f\u5f97\u5373\u4f7f\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\uff0cPython \u7a0b\u5e8f\u4e5f\u65e0\u6cd5\u5b9e\u73b0\u771f\u6b63\u7684\u5e76\u884c\u8ba1\u7b97\u3002GIL \u4f1a\u9501\u4f4f\u4e00\u4e2a\u7ebf\u7a0b\u7684\u6267\u884c\uff0c\u5bfc\u81f4\u5176\u4ed6\u7ebf\u7a0b\u65e0\u6cd5\u540c\u65f6\u6267\u884c Python \u5b57\u8282\u7801\u3002\u8fd9\u610f\u5473\u7740\u5728 CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e2d\uff0cPython \u7684\u591a\u7ebf\u7a0b\u4e0d\u80fd\u63d0\u9ad8\u6027\u80fd\uff0c\u53cd\u800c\u53ef\u80fd\u56e0\u4e3a\u4e0a\u4e0b\u6587\u5207\u6362\u5e26\u6765\u989d\u5916\u7684\u5f00\u9500\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u63a8\u8350\u4f7f\u7528\u591a\u8fdb\u7a0b\uff08<code>multiprocessing<\/code> \u6a21\u5757\uff09\u6765\u907f\u514d GIL \u7684\u9650\u5236\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u591a\u7ebf\u7a0b<\/strong> \u9002\u7528\u4e8e <strong>I\/O \u5bc6\u96c6\u578b\u4efb\u52a1<\/strong>\uff0c\u4f8b\u5982\u6587\u4ef6\u64cd\u4f5c\u3001\u7f51\u7edc\u8bf7\u6c42\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u7ebf\u7a0b\u6c60<\/strong> \u662f\u7ba1\u7406\u5927\u91cf\u7ebf\u7a0b\u7684\u9ad8\u6548\u65b9\u5f0f\uff0c\u907f\u514d\u4e86\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u7684\u5f00\u9500\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <code>Lock<\/code> \u6216 <code>RLock<\/code> \u6765\u4fdd\u8bc1\u7ebf\u7a0b\u95f4\u540c\u6b65\uff0c\u907f\u514d\u7ade\u6001\u6761\u4ef6\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0cPython \u7684 <strong>\u591a\u8fdb\u7a0b\u7f16\u7a0b<\/strong> \u66f4\u52a0\u5408\u9002\uff0c\u907f\u514d\u4e86 GIL \u7684\u9650\u5236\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python3 \u4e2d\uff0c\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u7528\u6765\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u5c24\u5176\u662f I\/O \u5bc6\u96c6\u578b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2876,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2875","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2875"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2875\/revisions"}],"predecessor-version":[{"id":2877,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2875\/revisions\/2877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2876"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}