{"id":1068480,"date":"2024-12-31T16:43:47","date_gmt":"2024-12-31T08:43:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068480.html"},"modified":"2024-12-31T16:43:50","modified_gmt":"2024-12-31T08:43:50","slug":"python%e6%96%87%e4%bb%b6%e4%b9%8b%e9%97%b4%e5%a6%82%e4%bd%95%e5%85%b1%e4%ba%ab%e5%90%8c%e4%b8%80%e5%8f%98%e9%87%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068480.html","title":{"rendered":"python\u6587\u4ef6\u4e4b\u95f4\u5982\u4f55\u5171\u4eab\u540c\u4e00\u53d8\u91cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/7aabf9e8-8ce3-4ecc-b094-c5d43ab426a8.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u6587\u4ef6\u4e4b\u95f4\u5982\u4f55\u5171\u4eab\u540c\u4e00\u53d8\u91cf\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u6587\u4ef6\u4e4b\u95f4\u5171\u4eab\u540c\u4e00\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u6a21\u5757\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3001\u4f7f\u7528\u6570\u636e\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u6a21\u5757\u3002<\/strong> \u5176\u4e2d\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u6a21\u5757\uff0c\u5e76\u5728\u9700\u8981\u7684\u6587\u4ef6\u4e2d\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u6a21\u5757\u6765\u5171\u4eab\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efa\u5171\u4eab\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2aPython\u6587\u4ef6\u6765\u5b58\u50a8\u5171\u4eab\u53d8\u91cf\u3002\u901a\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e2a\u6587\u4ef6\u547d\u540d\u4e3a<code>config.py<\/code>\u6216\u8005\u5176\u4ed6\u5177\u6709\u63cf\u8ff0\u6027\u7684\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>config.py<\/code>\u7684\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.py<\/p>\n<p>shared_variable = None<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u5bfc\u5165\u5171\u4eab\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728\u9700\u8981\u8bbf\u95ee\u6216\u4fee\u6539\u5171\u4eab\u53d8\u91cf\u7684\u6587\u4ef6\u4e2d\uff0c\u5bfc\u5165\u8fd9\u4e2a\u5171\u4eab\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u6709\u4e24\u4e2a\u6587\u4ef6<code>file1.py<\/code>\u548c<code>file2.py<\/code>\uff0c\u90fd\u9700\u8981\u5171\u4eab<code>config.py<\/code>\u4e2d\u7684\u53d8\u91cf<code>shared_variable<\/code>\u3002<\/p>\n<\/p>\n<p><h4>file1.py<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># file1.py<\/p>\n<p>import config<\/p>\n<p>def modify_shared_variable(value):<\/p>\n<p>    config.shared_variable = value<\/p>\n<p>    print(f&quot;Shared variable set to: {config.shared_variable}&quot;)<\/p>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    modify_shared_variable(&quot;Hello from file1&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>file2.py<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># file2.py<\/p>\n<p>import config<\/p>\n<p>def read_shared_variable():<\/p>\n<p>    print(f&quot;Shared variable is: {config.shared_variable}&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    read_shared_variable()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u9a8c\u8bc1\u5171\u4eab\u53d8\u91cf<\/h3>\n<\/p>\n<ol>\n<li>\n<p>\u5148\u8fd0\u884c<code>file1.py<\/code>\uff0c\u8bbe\u7f6e\u5171\u4eab\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">$ python file1.py<\/p>\n<p>Shared variable set to: Hello from file1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\u8fd0\u884c<code>file2.py<\/code>\uff0c\u8bfb\u53d6\u5171\u4eab\u53d8\u91cf\u7684\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">$ python file2.py<\/p>\n<p>Shared variable is: Hello from file1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4e24\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u53ef\u4ee5\u5171\u4eab\u548c\u4fee\u6539\u540c\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6a21\u5757\u7684\u5355\u4e00\u5b9e\u4f8b<\/strong>\uff1aPython\u6a21\u5757\u5728\u5bfc\u5165\u65f6\u4f1a\u88ab\u89e3\u91ca\u5668\u7f13\u5b58\uff0c\u56e0\u6b64\u5373\u4f7f\u5728\u4e0d\u540c\u7684\u6587\u4ef6\u4e2d\u591a\u6b21\u5bfc\u5165\u540c\u4e00\u4e2a\u6a21\u5757\uff0c\u5b9e\u9645\u4e0a\u4f7f\u7528\u7684\u90fd\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8fd9\u786e\u4fdd\u4e86\u5171\u4eab\u53d8\u91cf\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u5982\u679c\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4f7f\u7528\u5171\u4eab\u53d8\u91cf\uff0c\u9700\u8981\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\u3002\u53ef\u4ee5\u4f7f\u7528\u7ebf\u7a0b\u9501\uff08<code>threading.Lock<\/code>\uff09\u6765\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u53d8\u91cf\u65f6\u4e0d\u4f1a\u4ea7\u751f\u7ade\u6001\u6761\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u547d\u540d\u51b2\u7a81<\/strong>\uff1a\u786e\u4fdd\u6a21\u5757\u540d\u548c\u53d8\u91cf\u540d\u4e0d\u4e0e\u5176\u4ed6\u6a21\u5757\u51b2\u7a81\uff0c\u4ee5\u514d\u5f15\u8d77\u610f\u5916\u9519\u8bef\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u6a21\u5757\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u6587\u4ef6\u95f4\u53d8\u91cf\u5171\u4eab\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5c06\u5171\u4eab\u53d8\u91cf\u5b58\u50a8\u5728\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff08\u4f8b\u5982JSON\u3001YAML\uff09\uff0c\u5728\u9700\u8981\u4f7f\u7528\u7684\u6587\u4ef6\u4e2d\u8bfb\u53d6\u548c\u5199\u5165\u914d\u7f6e\u6587\u4ef6\u3002\u9002\u7528\u4e8e\u8f83\u590d\u6742\u7684\u914d\u7f6e\u548c\u6301\u4e45\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6570\u636e\u5e93<\/h4>\n<\/p>\n<p><p>\u5c06\u5171\u4eab\u53d8\u91cf\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u9002\u7528\u4e8e\u9700\u8981\u8de8\u8fdb\u7a0b\u6216\u5206\u5e03\u5f0f\u7cfb\u7edf\u5171\u4eab\u53d8\u91cf\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u5171\u4eab\u6570\u636e\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u4e14\u4e0d\u9891\u7e41\u53d8\u5316\u7684\u914d\u7f6e\u9879\u3002<\/p>\n<\/p>\n<p><h4>4. \u4f7f\u7528\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8de8\u8fdb\u7a0b\u901a\u4fe1\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982<code>multiprocessing<\/code>\u6a21\u5757\u4e2d\u7684\u961f\u5217\u6216\u7ba1\u9053\uff0c\u6216\u8005\u4f7f\u7528\u66f4\u9ad8\u7ea7\u7684\u6d88\u606f\u961f\u5217\u7cfb\u7edf\uff08\u5982RabbitMQ\u3001Redis\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6a21\u5757\u5171\u4eab\u53d8\u91cf\u662f\u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\uff0c\u56e0\u5176\u7b80\u5355\u3001\u76f4\u63a5\u4e14\u6613\u4e8e\u7ef4\u62a4\u3002\u5176\u4ed6\u65b9\u6cd5\u5219\u9002\u7528\u4e8e\u7279\u5b9a\u573a\u666f\uff0c\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u8fdb\u884c\u5b9e\u73b0\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u6ce8\u610f\u7ebf\u7a0b\u5b89\u5168\u548c\u6570\u636e\u4e00\u81f4\u6027\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684\u4e0d\u540c\u6587\u4ef6\u4e4b\u95f4\u5171\u4eab\u53d8\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5728\u4e0d\u540c\u6587\u4ef6\u4e4b\u95f4\u5171\u4eab\u53d8\u91cf\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u3002\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u53d8\u91cf\u7684\u6587\u4ef6\uff08\u4f8b\u5982<code>config.py<\/code>\uff09\uff0c\u5728\u5176\u4e2d\u5b9a\u4e49\u53d8\u91cf\uff0c\u7136\u540e\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u8fd9\u4e2a\u6a21\u5757\uff0c\u76f4\u63a5\u8bbf\u95ee\u53d8\u91cf\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6216\u51fd\u6570\u5c01\u88c5\u53d8\u91cf\uff0c\u4f7f\u5176\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u66f4\u6613\u4e8e\u7ba1\u7406\u548c\u4f7f\u7528\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u5171\u4eabPython\u6587\u4ef6\u4e2d\u7684\u53d8\u91cf\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u73af\u5883\u53d8\u91cf\u662f\u4e00\u79cd\u5171\u4eab\u4fe1\u606f\u7684\u6709\u6548\u65b9\u5f0f\u3002\u901a\u8fc7<code>os.environ<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u8bbf\u95ee\u548c\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u4e8e\u9700\u8981\u8de8\u8d8a\u591a\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5171\u4eab\u914d\u7f6e\u7684\u60c5\u51b5\u3002\u6ce8\u610f\uff0c\u8fd9\u6837\u7684\u53d8\u91cf\u901a\u5e38\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p><strong>\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u5171\u4eab\u5927\u578b\u6570\u636e\u7ed3\u6784\u65f6\u6709\u4ec0\u4e48\u5efa\u8bae\uff1f<\/strong><br \/>\u5171\u4eab\u5927\u578b\u6570\u636e\u7ed3\u6784\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528<code>pickle<\/code>\u6a21\u5757\u5c06\u6570\u636e\u5e8f\u5217\u5316\uff0c\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u53cd\u5e8f\u5217\u5316\u8bfb\u53d6\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u5185\u5b58\u5e76\u786e\u4fdd\u6570\u636e\u7ed3\u6784\u7684\u5b8c\u6574\u6027\u3002\u53e6\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u6216\u5916\u90e8\u5b58\u50a8\u89e3\u51b3\u65b9\u6848\uff08\u5982Redis\u6216MongoDB\uff09\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u5171\u4eab\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u6587\u4ef6\u4e4b\u95f4\u5171\u4eab\u540c\u4e00\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u6a21\u5757\u3001\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u3001\u4f7f\u7528\u6570\u636e\u5e93\u7b49\u3002\u6700\u5e38\u89c1\u4e14\u63a8\u8350 [&hellip;]","protected":false},"author":3,"featured_media":1068489,"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\/1068480"}],"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=1068480"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068480\/revisions"}],"predecessor-version":[{"id":1068490,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068480\/revisions\/1068490"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068489"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}