{"id":1097235,"date":"2025-01-08T15:09:43","date_gmt":"2025-01-08T07:09:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097235.html"},"modified":"2025-01-08T15:09:46","modified_gmt":"2025-01-08T07:09:46","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8d%e6%98%be%e7%a4%ba%e7%aa%97%e5%8f%a3%e5%90%8e%e5%8f%b0%e8%bf%90%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097235.html","title":{"rendered":"python\u5982\u4f55\u4e0d\u663e\u793a\u7a97\u53e3\u540e\u53f0\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212116\/1cefa504-f562-4c25-a303-b6ab2a2bc1b5.webp\" alt=\"python\u5982\u4f55\u4e0d\u663e\u793a\u7a97\u53e3\u540e\u53f0\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5728\u540e\u53f0\u8fd0\u884c\u7a0b\u5e8f\u800c\u4e0d\u663e\u793a\u7a97\u53e3\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528subprocess\u6a21\u5757\u3001\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\uff08Daemon\uff09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982pywin32\uff08\u4ec5\u9650Windows\u7cfb\u7edf\uff09\u7b49\u3002<\/strong>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528subprocess\u6a21\u5757<\/h3>\n<\/p>\n<p><h4>1\u3001subprocess\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Python\u7684subprocess\u6a21\u5757\u5141\u8bb8\u4f60\u751f\u6210\u65b0\u7684\u8fdb\u7a0b\u3001\u8fde\u63a5\u5176\u8f93\u5165\/\u8f93\u51fa\/\u9519\u8bef\u7ba1\u9053\uff0c\u5e76\u83b7\u5f97\u5176\u8fd4\u56de\u7801\u3002\u901a\u8fc7subprocess\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u540e\u53f0\u8fd0\u884c\u5176\u4ed6\u7a0b\u5e8f\u800c\u4e0d\u663e\u793a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5b50\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess.Popen<\/code>\u65b9\u6cd5\u8fd0\u884c\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7\u8bbe\u7f6e<code>creationflags<\/code>\u53c2\u6570\u6765\u9690\u85cf\u7a97\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>import os<\/p>\n<h2><strong>\u4f7f\u7528CREATE_NO_WINDOW\u6807\u5fd7\u6765\u9690\u85cf\u7a97\u53e3<\/strong><\/h2>\n<p>subprocess.Popen([&#39;python&#39;, &#39;your_script.py&#39;], creationflags=subprocess.CREATE_NO_WINDOW)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Unix\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6807\u51c6\u8f93\u5165\/\u8f93\u51fa\u91cd\u5b9a\u5411\u5230<code>\/dev\/null<\/code>\u6765\u5b9e\u73b0\u540e\u53f0\u8fd0\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import subprocess<\/p>\n<p>with open(&#39;\/dev\/null&#39;, &#39;w&#39;) as fnull:<\/p>\n<p>    subprocess.Popen([&#39;python&#39;, &#39;your_script.py&#39;], stdout=fnull, stderr=fnull, stdin=fnull)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u9519\u8bef\u5904\u7406<\/strong>\uff1a\u786e\u4fdd\u6355\u83b7\u548c\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u4e0d\u4f1a\u56e0\u4e3a\u672a\u5904\u7406\u7684\u5f02\u5e38\u800c\u5d29\u6e83\u3002<\/li>\n<li><strong><a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a><\/strong>\uff1a\u4f7f\u7528<code>subprocess<\/code>\u65f6\uff0c\u786e\u4fdd\u6b63\u786e\u7ba1\u7406\u5b50\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\uff08Daemon\uff09<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b88\u62a4\u8fdb\u7a0b\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>\u5b88\u62a4\u8fdb\u7a0b\u662f\u6307\u5728\u540e\u53f0\u8fd0\u884c\u4e14\u4e0d\u4e0e\u4efb\u4f55\u63a7\u5236\u7ec8\u7aef\u76f8\u5173\u8054\u7684\u8fdb\u7a0b\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u6267\u884c\u957f\u671f\u4efb\u52a1\u800c\u4e0d\u9700\u8981\u7528\u6237\u5e72\u9884\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b<\/h4>\n<\/p>\n<p><p>\u5728Unix\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os.fork<\/code>\u65b9\u6cd5\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>import sys<\/p>\n<p>def daemonize():<\/p>\n<p>    if os.fork() &gt; 0:<\/p>\n<p>        sys.exit(0)<\/p>\n<p>    os.setsid()<\/p>\n<p>    if os.fork() &gt; 0:<\/p>\n<p>        sys.exit(0)<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    while True:<\/p>\n<p>        # \u4f60\u7684\u4ee3\u7801\u903b\u8f91<\/p>\n<p>        pass<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    daemonize()<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<code>python-daemon<\/code>\u6765\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from daemon import DaemonContext<\/p>\n<p>def main():<\/p>\n<p>    while True:<\/p>\n<p>        # \u4f60\u7684\u4ee3\u7801\u903b\u8f91<\/p>\n<p>        pass<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    with DaemonContext():<\/p>\n<p>        main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u4fe1\u53f7\u5904\u7406<\/strong>\uff1a\u786e\u4fdd\u6b63\u786e\u5904\u7406\u4fe1\u53f7\uff0c\u5982\u7ec8\u6b62\u4fe1\u53f7\uff08SIGTERM\uff09\uff0c\u4ee5\u4fbf\u7a0b\u5e8f\u80fd\u591f\u4f18\u96c5\u5730\u9000\u51fa\u3002<\/li>\n<li><strong>\u6587\u4ef6\u63cf\u8ff0\u7b26<\/strong>\uff1a\u5173\u95ed\u4e0d\u5fc5\u8981\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982pywin32\uff09<\/h3>\n<\/p>\n<p><h4>1\u3001pywin32\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>pywin32\u662f\u4e00\u4e2aPython\u6269\u5c55\u5305\uff0c\u63d0\u4f9b\u4e86\u5bf9Windows\u64cd\u4f5c\u7cfb\u7edfAPI\u7684\u8bbf\u95ee\u3002\u901a\u8fc7pywin32\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u9690\u85cf\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u9690\u85cf\u7a97\u53e3<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528pywin32\u9690\u85cf\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32gui<\/p>\n<p>import win32con<\/p>\n<p>def hide_console_window():<\/p>\n<p>    hWnd = win32gui.GetForegroundWindow()<\/p>\n<p>    win32gui.ShowWindow(hWnd, win32con.SW_HIDE)<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    hide_console_window()<\/p>\n<p>    # \u4f60\u7684\u4ee3\u7801\u903b\u8f91<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<ul>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u786e\u4fddpywin32\u5e93\u7684\u7248\u672c\u4e0ePython\u7248\u672c\u517c\u5bb9\u3002<\/li>\n<li><strong>\u4f9d\u8d56\u7ba1\u7406<\/strong>\uff1a\u5728\u9879\u76ee\u4e2d\u4f7f\u7528pywin32\u65f6\uff0c\u786e\u4fdd\u6b63\u786e\u7ba1\u7406\u4f9d\u8d56\u5e93\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528subprocess\u6a21\u5757\u3001\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\u4ee5\u53ca\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\uff08\u5982pywin32\uff09\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u540e\u53f0\u8fd0\u884c\u7a0b\u5e8f\u800c\u4e0d\u663e\u793a\u7a97\u53e3\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u4e4b\uff0csubprocess\u6a21\u5757\u9002\u5408\u8de8\u5e73\u53f0\u4f7f\u7528\u3001\u5b88\u62a4\u8fdb\u7a0b\u9002\u5408\u957f\u671f\u4efb\u52a1\u3001pywin32\u9002\u5408Windows\u5e73\u53f0\u3002<\/strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u9519\u8bef\u548c\u7ba1\u7406\u8d44\u6e90\uff0c\u4ee5\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\u4e2d\u5b9e\u73b0\u540e\u53f0\u8fd0\u884c\u800c\u4e0d\u663e\u793a\u7a97\u53e3\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u6765\u8fd0\u884c\u811a\u672c\u800c\u4e0d\u663e\u793a\u4efb\u4f55\u7a97\u53e3\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u8bbe\u7f6e<code>creationflags<\/code>\u53c2\u6570\u4e3a<code>subprocess.CREATE_NO_WINDOW<\/code>\u3002\u8fd9\u6837\uff0c\u7a0b\u5e8f\u5c06\u4f1a\u5728\u540e\u53f0\u8fd0\u884c\uff0c\u7528\u6237\u4e0d\u4f1a\u770b\u5230\u4efb\u4f55\u7a97\u53e3\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u811a\u672c\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u4e0d\u5e72\u6270\u7528\u6237\u754c\u9762\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fddPython\u811a\u672c\u5728\u540e\u53f0\u8fd0\u884c\u800c\u4e0d\u5e72\u6270\u7528\u6237\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>nohup<\/code>\u547d\u4ee4\uff08\u5728Unix\/Linux\u7cfb\u7edf\u4e0a\uff09\u6216\u8005Windows\u7684\u4efb\u52a1\u8ba1\u5212\u7a0b\u5e8f\u6765\u8bbe\u7f6e\u811a\u672c\u3002\u8fd9\u6837\uff0c\u811a\u672c\u7684\u6267\u884c\u4e0d\u4f1a\u5e72\u6270\u7528\u6237\u7684\u64cd\u4f5c\uff0c\u4e14\u53ef\u4ee5\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u65b9\u6cd5\u5b9e\u73b0Python\u7a0b\u5e8f\u7684\u9690\u5f62\u8fd0\u884c\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>subprocess<\/code>\u6a21\u5757\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5de5\u5177\u5982<code>pyinstaller<\/code>\u5c06Python\u811a\u672c\u6253\u5305\u6210\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u5e76\u5728\u6253\u5305\u65f6\u9009\u62e9\u201c\u65e0\u7a97\u53e3\u201d\u9009\u9879\u3002\u8fd9\u6837\u751f\u6210\u7684\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u5c06\u4e0d\u4f1a\u663e\u793a\u4efb\u4f55\u7a97\u53e3\uff0c\u9002\u5408\u9700\u8981\u540e\u53f0\u670d\u52a1\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u5728\u540e\u53f0\u8fd0\u884c\u7a0b\u5e8f\u800c\u4e0d\u663e\u793a\u7a97\u53e3\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528subprocess\u6a21\u5757\u3001\u521b\u5efa [&hellip;]","protected":false},"author":3,"featured_media":1097239,"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\/1097235"}],"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=1097235"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097235\/revisions"}],"predecessor-version":[{"id":1097240,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097235\/revisions\/1097240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097239"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}