{"id":1109777,"date":"2025-01-08T17:13:23","date_gmt":"2025-01-08T09:13:23","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109777.html"},"modified":"2025-01-08T17:13:29","modified_gmt":"2025-01-08T09:13:29","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9%e4%b8%a4%e4%b8%aapython%e7%a8%8b%e5%ba%8f%e6%9c%89%e5%85%b3%e8%81%94","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109777.html","title":{"rendered":"\u5982\u4f55\u8ba9\u4e24\u4e2apython\u7a0b\u5e8f\u6709\u5173\u8054"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072918\/9ad2c688-8889-402d-8a60-3831b4e61af8.webp\" alt=\"\u5982\u4f55\u8ba9\u4e24\u4e2apython\u7a0b\u5e8f\u6709\u5173\u8054\" \/><\/p>\n<p><p> <strong>\u8981\u8ba9\u4e24\u4e2aPython\u7a0b\u5e8f\u6709\u5173\u8054\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u6587\u4ef6\u5171\u4eab\u3001\u6570\u636e\u5e93\u5171\u4eab\u3001\u7f51\u7edc\u901a\u4fe1\u3001\u5171\u4eab\u5185\u5b58\u3002<\/strong> \u5176\u4e2d\uff0c\u7f51\u7edc\u901a\u4fe1\u662f\u4e00\u79cd\u5e38\u7528\u4e14\u7075\u6d3b\u7684\u65b9\u6cd5\u3002\u7f51\u7edc\u901a\u4fe1\u53ef\u4ee5\u901a\u8fc7\u5957\u63a5\u5b57\u7f16\u7a0b\u6765\u5b9e\u73b0\uff0c\u80fd\u591f\u8ba9\u4e24\u4e2a\u7a0b\u5e8f\u5728\u540c\u4e00\u53f0\u673a\u5668\u4e0a\u6216\u8005\u4e0d\u540c\u673a\u5668\u4e0a\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6587\u4ef6\u5171\u4eab<\/h2>\n<\/p>\n<p><p>\u6587\u4ef6\u5171\u4eab\u662f\u4e00\u79cd\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u6765\u5b9e\u73b0\u4e24\u4e2a\u7a0b\u5e8f\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u3002\u4e00\u4e2a\u7a0b\u5e8f\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1. \u5199\u5165\u6570\u636e\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># writer.py<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello from Program 1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bfb\u53d6\u6570\u636e\u4ece\u6587\u4ef6<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># reader.py<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u884c\uff0c\u4f46\u53ef\u80fd\u5b58\u5728\u591a\u4e2a\u7a0b\u5e8f\u540c\u65f6\u5199\u5165\u6587\u4ef6\u65f6\u7684\u7ade\u4e89\u95ee\u9898\uff0c\u9700\u8981\u989d\u5916\u7684\u9501\u673a\u5236\u6765\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u6570\u636e\u5e93\u5171\u4eab<\/h2>\n<\/p>\n<p><p>\u6570\u636e\u5e93\u5171\u4eab\u662f\u901a\u8fc7\u4e00\u4e2a\u5171\u4eab\u7684\u6570\u636e\u5e93\u6765\u5b9e\u73b0\u4e24\u4e2a\u7a0b\u5e8f\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u3002\u5e38\u7528\u7684\u6570\u636e\u5e93\u6709SQLite\u3001MySQL\u3001PostgreSQL\u7b49\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u548c\u5199\u5165\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># writer.py<\/p>\n<p>import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>Create table<\/strong><\/h2>\n<p>c.execute(&#39;&#39;&#39;CREATE TABLE IF NOT EXISTS data<\/p>\n<p>             (content TEXT)&#39;&#39;&#39;)<\/p>\n<h2><strong>Insert a row of data<\/strong><\/h2>\n<p>c.execute(&quot;INSERT INTO data (content) VALUES (&#39;Hello from Program 1&#39;)&quot;)<\/p>\n<h2><strong>Save (commit) the changes<\/strong><\/h2>\n<p>conn.commit()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8bfb\u53d6\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># reader.py<\/p>\n<p>import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>c = conn.cursor()<\/p>\n<h2><strong>Retrieve data<\/strong><\/h2>\n<p>for row in c.execute(&#39;SELECT * FROM data&#39;):<\/p>\n<p>    print(row)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6570\u636e\u5e93\u65b9\u5f0f\u8f83\u4e3a\u7a33\u5065\uff0c\u9002\u5408\u6570\u636e\u91cf\u5927\u4e14\u9700\u8981\u6301\u4e45\u5316\u5b58\u50a8\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u7f51\u7edc\u901a\u4fe1<\/h2>\n<\/p>\n<p><p>\u7f51\u7edc\u901a\u4fe1\u662f\u901a\u8fc7\u7f51\u7edc\u534f\u8bae\uff08\u5982TCP\/IP\uff09\u5b9e\u73b0\u4e24\u4e2a\u7a0b\u5e8f\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u3002\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>socket<\/code>\u5e93\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>1. \u670d\u52a1\u5668\u7aef<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># server.py<\/p>\n<p>import socket<\/p>\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>s.bind((&#39;localhost&#39;, 12345))<\/p>\n<p>s.listen(5)<\/p>\n<p>while True:<\/p>\n<p>    conn, addr = s.accept()<\/p>\n<p>    print(&#39;Connected by&#39;, addr)<\/p>\n<p>    data = conn.recv(1024)<\/p>\n<p>    if not data:<\/p>\n<p>        break<\/p>\n<p>    print(&#39;Received&#39;, repr(data))<\/p>\n<p>    conn.sendall(b&#39;Hello from Server&#39;)<\/p>\n<p>    conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5ba2\u6237\u7aef<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># client.py<\/p>\n<p>import socket<\/p>\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p>\n<p>s.connect((&#39;localhost&#39;, 12345))<\/p>\n<p>s.sendall(b&#39;Hello from Client&#39;)<\/p>\n<p>data = s.recv(1024)<\/p>\n<p>s.close()<\/p>\n<p>print(&#39;Received&#39;, repr(data))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7f51\u7edc\u901a\u4fe1\u65b9\u5f0f\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u5206\u5e03\u5f0f\u7cfb\u7edf\u548c\u7f51\u7edc\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5171\u4eab\u5185\u5b58<\/h2>\n<\/p>\n<p><p>\u5171\u4eab\u5185\u5b58\u662f\u901a\u8fc7\u5728\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u5171\u4eab\u5185\u5b58\u533a\u57df\u6765\u5b9e\u73b0\u6570\u636e\u5171\u4eab\u3002Python\u7684<code>multiprocessing<\/code>\u5e93\u63d0\u4f9b\u4e86\u5171\u4eab\u5185\u5b58\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>1. \u4f7f\u7528\u5171\u4eab\u5185\u5b58<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\"># shared_memory.py<\/p>\n<p>from multiprocessing import Process, Value<\/p>\n<p>def modify_shared_memory(shared_value):<\/p>\n<p>    shared_value.value += 1<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    shared_value = Value(&#39;i&#39;, 0)<\/p>\n<p>    p1 = Process(target=modify_shared_memory, args=(shared_value,))<\/p>\n<p>    p2 = Process(target=modify_shared_memory, args=(shared_value,))<\/p>\n<p>    p1.start()<\/p>\n<p>    p2.start()<\/p>\n<p>    p1.join()<\/p>\n<p>    p2.join()<\/p>\n<p>    print(shared_value.value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5171\u4eab\u5185\u5b58\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u9ad8\u6548\u6570\u636e\u4f20\u9012\u7684\u591a\u8fdb\u7a0b\u5e94\u7528\uff0c\u4f46\u9700\u8981\u5c0f\u5fc3\u5904\u7406\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e24\u4e2aPython\u7a0b\u5e8f\u4e4b\u95f4\u7684\u5173\u8054\u548c\u6570\u636e\u4f20\u9012\u3002<strong>\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5c24\u4e3a\u91cd\u8981<\/strong>\u3002\u4f8b\u5982\uff0c\u6587\u4ef6\u5171\u4eab\u9002\u5408\u7b80\u5355\u7684\u6570\u636e\u4f20\u9012\uff0c\u6570\u636e\u5e93\u9002\u5408\u6301\u4e45\u5316\u5b58\u50a8\u548c\u67e5\u8be2\uff0c\u7f51\u7edc\u901a\u4fe1\u9002\u5408\u5206\u5e03\u5f0f\u7cfb\u7edf\uff0c\u5171\u4eab\u5185\u5b58\u9002\u5408\u9ad8\u6548\u7684\u591a\u8fdb\u7a0b\u6570\u636e\u4f20\u9012\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u8003\u8651\u6570\u636e\u4e00\u81f4\u6027\u548c\u5e76\u53d1\u5904\u7406\u7684\u95ee\u9898\uff0c\u786e\u4fdd\u4e24\u4e2a\u7a0b\u5e8f\u80fd\u591f\u7a33\u5b9a\u3001\u53ef\u9760\u5730\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u4e24\u4e2aPython\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\u6570\u636e\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5728\u4e24\u4e2aPython\u7a0b\u5e8f\u4e4b\u95f4\u5171\u4eab\u6570\u636e\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u8bfb\u53d6\u548c\u5199\u5165\u6570\u636e\u3001\u4f7f\u7528\u6570\u636e\u5e93\u8fdb\u884c\u6570\u636e\u5b58\u50a8\u3001\u901a\u8fc7\u7f51\u7edc\u8bf7\u6c42\u5b9e\u73b0\u8fdc\u7a0b\u901a\u4fe1\uff0c\u4ee5\u53ca\u5229\u7528\u6d88\u606f\u961f\u5217\u6765\u8fdb\u884c\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002\u9009\u62e9\u9002\u5408\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u5927\u5c0f\u3001\u5b9e\u65f6\u6027\u9700\u6c42\u53ca\u7a0b\u5e8f\u7684\u590d\u6742\u6027\u3002<\/p>\n<p><strong>\u5728\u4e24\u4e2aPython\u7a0b\u5e8f\u4e2d\u5982\u4f55\u5b9e\u73b0\u51fd\u6570\u8c03\u7528\uff1f<\/strong><br \/>\u5b9e\u73b0\u51fd\u6570\u8c03\u7528\u7684\u5e38\u89c1\u65b9\u5f0f\u662f\u5c06\u9700\u8981\u5171\u4eab\u7684\u51fd\u6570\u653e\u5165\u4e00\u4e2a\u6a21\u5757\u4e2d\uff0c\u7136\u540e\u5728\u4e24\u4e2a\u7a0b\u5e8f\u4e2d\u5bfc\u5165\u8be5\u6a21\u5757\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u6a21\u5757\u4e2d\u7684\u51fd\u6570\uff0c\u65e0\u9700\u91cd\u590d\u4ee3\u7801\u3002\u6b64\u5916\uff0c\u4f7f\u7528REST API\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7HTTP\u8bf7\u6c42\u5728\u7a0b\u5e8f\u95f4\u8c03\u7528\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8de8\u7f51\u7edc\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u679c\u4e24\u4e2aPython\u7a0b\u5e8f\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\uff0c\u5982\u4f55\u4fdd\u6301\u5b83\u4eec\u7684\u5173\u8054\uff1f<\/strong><br \/>\u5728\u4e0d\u540c\u673a\u5668\u4e0a\u8fd0\u884c\u7684Python\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u7f51\u7edc\u8fdb\u884c\u901a\u4fe1\uff0c\u4f8b\u5982\u4f7f\u7528HTTP\u8bf7\u6c42\u3001WebSocket\u6216\u6d88\u606f\u961f\u5217\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u5728\u7a0b\u5e8f\u95f4\u7684\u5b9e\u65f6\u66f4\u65b0\uff0c\u4fdd\u6301\u5b83\u4eec\u7684\u5173\u8054\u6027\u3002\u4f7f\u7528\u4e91\u670d\u52a1\u548cAPI\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u80fd\u7b80\u5316\u8de8\u673a\u5668\u7684\u4ea4\u4e92\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u8ba9\u4e24\u4e2aPython\u7a0b\u5e8f\u6709\u5173\u8054\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u6587\u4ef6\u5171\u4eab\u3001\u6570\u636e\u5e93\u5171\u4eab\u3001\u7f51\u7edc\u901a\u4fe1\u3001\u5171\u4eab\u5185\u5b58\u3002 \u5176\u4e2d\uff0c\u7f51\u7edc\u901a\u4fe1\u662f [&hellip;]","protected":false},"author":3,"featured_media":1109791,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109777"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1109777"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109777\/revisions"}],"predecessor-version":[{"id":1109793,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109777\/revisions\/1109793"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109791"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109777"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109777"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109777"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}