{"id":1104963,"date":"2025-01-08T16:26:27","date_gmt":"2025-01-08T08:26:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104963.html"},"modified":"2025-01-08T16:26:29","modified_gmt":"2025-01-08T08:26:29","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e6%b7%bb%e5%8a%a0%e5%88%b0%e7%b3%bb%e7%bb%9f%e6%9c%8d%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104963.html","title":{"rendered":"\u5982\u4f55\u5c06python\u6dfb\u52a0\u5230\u7cfb\u7edf\u670d\u52a1"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070103\/469214d0-edce-47dd-9214-6ba4d67a1a13.webp\" alt=\"\u5982\u4f55\u5c06python\u6dfb\u52a0\u5230\u7cfb\u7edf\u670d\u52a1\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5c06Python\u6dfb\u52a0\u5230\u7cfb\u7edf\u670d\u52a1<\/strong><\/p>\n<\/p>\n<p><p>\u5728Windows\u3001Linux\u6216macOS\u7cfb\u7edf\u4e2d\uff0c\u5c06Python\u811a\u672c\u6dfb\u52a0\u4e3a\u7cfb\u7edf\u670d\u52a1\u53ef\u4ee5\u5b9e\u73b0\u81ea\u52a8\u542f\u52a8\u548c\u540e\u53f0\u8fd0\u884c\u529f\u80fd\u3002<strong>\u5b89\u88c5\u5fc5\u8981\u5de5\u5177\u3001\u7f16\u5199\u670d\u52a1\u6587\u4ef6\u3001\u6ce8\u518c\u670d\u52a1\u3001\u542f\u52a8\u548c\u7ba1\u7406\u670d\u52a1<\/strong>\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728\u4efb\u4f55\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5c06Python\u811a\u672c\u6dfb\u52a0\u4e3a\u7cfb\u7edf\u670d\u52a1\u90fd\u9700\u8981\u4e00\u4e9b\u5de5\u5177\u548c\u5e93\u3002\u9996\u5148\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u4e0a\u5df2\u5b89\u88c5Python\uff0c\u5e76\u4e14\u4f60\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\u5176\u4ed6\u5fc5\u8981\u7684\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Windows\u4e0a\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b89\u88c5<code>pywin32<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bbf\u95eeWindows API\u7684\u63a5\u53e3\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Linux\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528<code>systemd<\/code>\u6765\u7ba1\u7406\u7cfb\u7edf\u670d\u52a1\u3002\u786e\u4fdd\u4f60\u7684Linux\u53d1\u884c\u7248\u652f\u6301<code>systemd<\/code>\uff0c\u5927\u591a\u6570\u73b0\u4ee3Linux\u53d1\u884c\u7248\u90fd\u9ed8\u8ba4\u652f\u6301\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>macOS\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728macOS\u4e0a\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>launchd<\/code>\u6765\u7ba1\u7406\u7cfb\u7edf\u670d\u52a1\u3002\u5b83\u662fmacOS\u7684\u5185\u7f6e\u670d\u52a1\u7ba1\u7406\u5de5\u5177\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u7f16\u5199\u670d\u52a1\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u7684\u4e0d\u540c\uff0c\u670d\u52a1\u6587\u4ef6\u7684\u7f16\u5199\u65b9\u5f0f\u4e5f\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aPython\u811a\u672c\u6765\u6ce8\u518c\u548c\u542f\u52a8\u670d\u52a1\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_service.py<\/code>\u7684\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32serviceutil<\/p>\n<p>import win32service<\/p>\n<p>import win32event<\/p>\n<p>import servicemanager<\/p>\n<p>class MyService(win32serviceutil.ServiceFramework):<\/p>\n<p>    _svc_name_ = &quot;MyService&quot;<\/p>\n<p>    _svc_display_name_ = &quot;My Python Service&quot;<\/p>\n<p>    _svc_description_ = &quot;This is a Python service example.&quot;<\/p>\n<p>    def __init__(self, args):<\/p>\n<p>        win32serviceutil.ServiceFramework.__init__(self, args)<\/p>\n<p>        self.hW<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>tStop = win32event.CreateEvent(None, 0, 0, None)<\/p>\n<p>    def SvcStop(self):<\/p>\n<p>        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)<\/p>\n<p>        win32event.SetEvent(self.hWaitStop)<\/p>\n<p>    def SvcDoRun(self):<\/p>\n<p>        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,<\/p>\n<p>                              servicemanager.PYS_SERVICE_STARTED,<\/p>\n<p>                              (self._svc_name_, &#39;&#39;))<\/p>\n<p>        self.main()<\/p>\n<p>    def main(self):<\/p>\n<p>        # \u8fd9\u91cc\u7f16\u5199\u4f60\u7684\u670d\u52a1\u903b\u8f91<\/p>\n<p>        pass<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    win32serviceutil.HandleCommandLine(MyService)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Linux\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>systemd<\/code>\u670d\u52a1\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>my_service.service<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=My Python Service<\/p>\n<p>After=network.target<\/p>\n<p>[Service]<\/p>\n<p>ExecStart=\/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p>Restart=always<\/p>\n<p>User=nobody<\/p>\n<p>Group=nogroup<\/p>\n<p>[Install]<\/p>\n<p>WantedBy=multi-user.target<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>macOS\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728macOS\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a<code>launchd<\/code>\u670d\u52a1\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>com.example.myservice.plist<\/code>\u7684\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<\/p>\n<p>&lt;!DOCTYPE plist PUBLIC &quot;-\/\/Apple Computer\/\/DTD PLIST 1.0\/\/EN&quot; &quot;http:\/\/www.apple.com\/DTDs\/PropertyList-1.0.dtd&quot;&gt;<\/p>\n<p>&lt;plist version=&quot;1.0&quot;&gt;<\/p>\n<p>&lt;dict&gt;<\/p>\n<p>    &lt;key&gt;Label&lt;\/key&gt;<\/p>\n<p>    &lt;string&gt;com.example.myservice&lt;\/string&gt;<\/p>\n<p>    &lt;key&gt;ProgramArguments&lt;\/key&gt;<\/p>\n<p>    &lt;array&gt;<\/p>\n<p>        &lt;string&gt;\/usr\/bin\/python3&lt;\/string&gt;<\/p>\n<p>        &lt;string&gt;\/path\/to\/your_script.py&lt;\/string&gt;<\/p>\n<p>    &lt;\/array&gt;<\/p>\n<p>    &lt;key&gt;RunAtLoad&lt;\/key&gt;<\/p>\n<p>    &lt;true\/&gt;<\/p>\n<p>    &lt;key&gt;KeepAlive&lt;\/key&gt;<\/p>\n<p>    &lt;true\/&gt;<\/p>\n<p>&lt;\/dict&gt;<\/p>\n<p>&lt;\/plist&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u6ce8\u518c\u670d\u52a1<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u63d0\u793a\u7b26\uff08\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\uff09\u6765\u6ce8\u518c\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python my_service.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Linux\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u5c06\u670d\u52a1\u6587\u4ef6\u590d\u5236\u5230<code>\/etc\/systemd\/system<\/code>\u76ee\u5f55\uff0c\u7136\u540e\u542f\u7528\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo cp my_service.service \/etc\/systemd\/system\/<\/p>\n<p>sudo systemctl enable my_service<\/p>\n<p>sudo systemctl start my_service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>macOS\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728macOS\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u5c06<code>plist<\/code>\u6587\u4ef6\u590d\u5236\u5230<code>~\/Library\/LaunchAgents<\/code>\u6216<code>\/Library\/LaunchDaemons<\/code>\u76ee\u5f55\uff0c\u7136\u540e\u52a0\u8f7d\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">cp com.example.myservice.plist ~\/Library\/LaunchAgents\/<\/p>\n<p>launchctl load ~\/Library\/LaunchAgents\/com.example.myservice.plist<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u542f\u52a8\u548c\u7ba1\u7406\u670d\u52a1<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Windows\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Windows\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u670d\u52a1\u7ba1\u7406\u5668\u6216\u547d\u4ee4\u884c\u6765\u542f\u52a8\u548c\u7ba1\u7406\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">net start MyService<\/p>\n<p>net stop MyService<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Linux\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7<code>systemctl<\/code>\u547d\u4ee4\u6765\u7ba1\u7406\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo systemctl start my_service<\/p>\n<p>sudo systemctl stop my_service<\/p>\n<p>sudo systemctl status my_service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>macOS\u7cfb\u7edf<\/strong><\/p>\n<p>\u5728macOS\u7cfb\u7edf\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7<code>launchctl<\/code>\u547d\u4ee4\u6765\u7ba1\u7406\u670d\u52a1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">launchctl start com.example.myservice<\/p>\n<p>launchctl stop com.example.myservice<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5c06Python\u811a\u672c\u6dfb\u52a0\u4e3a\u7cfb\u7edf\u670d\u52a1\u53ef\u4ee5\u4f7f\u5176\u5728\u7cfb\u7edf\u542f\u52a8\u65f6\u81ea\u52a8\u8fd0\u884c\uff0c\u5e76\u5728\u540e\u53f0\u6301\u7eed\u5de5\u4f5c\u3002<strong>\u5b89\u88c5\u5fc5\u8981\u5de5\u5177\u3001\u7f16\u5199\u670d\u52a1\u6587\u4ef6\u3001\u6ce8\u518c\u670d\u52a1\u3001\u542f\u52a8\u548c\u7ba1\u7406\u670d\u52a1<\/strong>\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b9e\u73b0\u65b9\u5f0f\u6709\u6240\u4e0d\u540c\uff0c\u4f46\u603b\u4f53\u6d41\u7a0b\u76f8\u4f3c\u3002\u901a\u8fc7\u6b63\u786e\u914d\u7f6e\u548c\u7ba1\u7406\u7cfb\u7edf\u670d\u52a1\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8Python\u811a\u672c\u7684\u81ea\u52a8\u5316\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u811a\u672c\u8bbe\u7f6e\u4e3aWindows\u7cfb\u7edf\u670d\u52a1\uff1f<\/strong><br \/>\u8981\u5c06Python\u811a\u672c\u8bbe\u7f6e\u4e3aWindows\u7cfb\u7edf\u670d\u52a1\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u540d\u4e3a\u201cNSSM\uff08Non-Sucking Service Manager\uff09\u201d\u7684\u5de5\u5177\u3002\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u4e0b\u8f7dNSSM\uff0c\u5e76\u4f7f\u7528\u547d\u4ee4\u884c\u5de5\u5177\u6307\u5b9a\u60a8\u7684Python\u53ef\u6267\u884c\u6587\u4ef6\u548c\u811a\u672c\u8def\u5f84\u3002\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7Windows\u670d\u52a1\u7ba1\u7406\u5668\u6765\u542f\u52a8\u3001\u505c\u6b62\u6216\u81ea\u52a8\u542f\u52a8\u8be5\u670d\u52a1\u3002<\/p>\n<p><strong>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u5982\u4f55\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u4f5c\u4e3a\u670d\u52a1\u8fd0\u884c\uff1f<\/strong><br \/>\u5728Linux\u4e0a\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2asystemd\u670d\u52a1\u5355\u5143\u6587\u4ef6\u6765\u5c06Python\u5e94\u7528\u7a0b\u5e8f\u4f5c\u4e3a\u670d\u52a1\u8fd0\u884c\u3002\u60a8\u9700\u8981\u5728<code>\/etc\/systemd\/system\/<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a<code>.service<\/code>\u6587\u4ef6\uff0c\u6307\u5b9a\u670d\u52a1\u7684\u63cf\u8ff0\u3001\u6267\u884c\u547d\u4ee4\u548c\u5176\u4ed6\u53c2\u6570\u3002\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u8fd0\u884c\u547d\u4ee4\u201csystemctl start your_service_name\u201d\u6765\u542f\u52a8\u670d\u52a1\uff0c\u5e76\u4f7f\u7528\u201csystemctl enable your_service_name\u201d\u6765\u8bbe\u7f6e\u5176\u5f00\u673a\u81ea\u542f\u3002<\/p>\n<p><strong>\u5bf9\u4e8e\u521d\u5b66\u8005\uff0c\u5982\u4f55\u68c0\u6d4bPython\u670d\u52a1\u662f\u5426\u6b63\u5e38\u8fd0\u884c\uff1f<\/strong><br \/>\u68c0\u6d4bPython\u670d\u52a1\u662f\u5426\u6b63\u5e38\u8fd0\u884c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u53ef\u4ee5\u5728\u4efb\u52a1\u7ba1\u7406\u5668\u4e2d\u67e5\u770b\u670d\u52a1\u5217\u8868\uff0c\u5e76\u786e\u8ba4Python\u670d\u52a1\u7684\u72b6\u6001\u3002Linux\u7528\u6237\u5219\u53ef\u4ee5\u4f7f\u7528\u201csystemctl status your_service_name\u201d\u547d\u4ee4\u6765\u67e5\u770b\u670d\u52a1\u7684\u5f53\u524d\u72b6\u6001\u548c\u65e5\u5fd7\u4fe1\u606f\uff0c\u786e\u4fdd\u670d\u52a1\u6ca1\u6709\u62a5\u9519\u5e76\u6b63\u5e38\u8fd0\u884c\u3002\u7ed3\u5408\u65e5\u5fd7\u6587\u4ef6\u8fdb\u884c\u6392\u67e5\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5c06Python\u6dfb\u52a0\u5230\u7cfb\u7edf\u670d\u52a1 \u5728Windows\u3001Linux\u6216macOS\u7cfb\u7edf\u4e2d\uff0c\u5c06Python\u811a\u672c\u6dfb\u52a0\u4e3a\u7cfb\u7edf [&hellip;]","protected":false},"author":3,"featured_media":1104970,"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\/1104963"}],"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=1104963"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104963\/revisions"}],"predecessor-version":[{"id":1104972,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104963\/revisions\/1104972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104970"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}