{"id":1083734,"date":"2025-01-08T13:00:53","date_gmt":"2025-01-08T05:00:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1083734.html"},"modified":"2025-01-08T13:00:56","modified_gmt":"2025-01-08T05:00:56","slug":"%e5%a6%82%e4%bd%95%e4%bd%bfpython%e7%a8%8b%e5%ba%8f%e4%b8%80%e7%9b%b4%e8%bf%90%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1083734.html","title":{"rendered":"\u5982\u4f55\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194138\/cb7852f3-915d-4071-9732-a66fea6c2088.webp\" alt=\"\u5982\u4f55\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\" \/><\/p>\n<p><p> <strong>\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3001\u4f7f\u7528\u5b9a\u65f6\u5668\u3001\u4f7f\u7528\u5b88\u62a4\u8fdb\u7a0b\u3001\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u7b49<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u5f15\u5165<code>while True<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u4e0d\u65ad\u6267\u884c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u6765\u4fdd\u6301Python\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u65e0\u9650\u5faa\u73af<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u662f\u6700\u7b80\u5355\u4e14\u5e38\u89c1\u7684\u65b9\u5f0f\u6765\u786e\u4fddPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u5f15\u5165<code>while True<\/code>\u5faa\u73af\uff0c\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u4e0d\u65ad\u6267\u884c\u6307\u5b9a\u7684\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    # \u6267\u884c\u4efb\u52a1<\/p>\n<p>    print(&quot;\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>    # \u6682\u505c\u4e00\u6bb5\u65f6\u95f4\uff0c\u4ee5\u514d\u5360\u7528\u8fc7\u591a\u7684CPU\u8d44\u6e90<\/p>\n<p>    time.sleep(5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>while True<\/code>\u5faa\u73af\u786e\u4fdd\u7a0b\u5e8f\u4e0d\u65ad\u8f93\u51fa\u201c\u7a0b\u5e8f\u6b63\u5728\u8fd0\u884c\u201d\u8fd9\u53e5\u8bdd\u3002\u4e3a\u4e86\u907f\u514d\u5360\u7528\u8fc7\u591a\u7684CPU\u8d44\u6e90\uff0c\u4f7f\u7528\u4e86<code>time.sleep(5)<\/code>\u8ba9\u7a0b\u5e8f\u6bcf5\u79d2\u949f\u6682\u505c\u4e00\u6b21\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b9a\u65f6\u5668<\/h3>\n<\/p>\n<p><p>\u5b9a\u65f6\u5668\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u5185\u91cd\u590d\u6267\u884c\u67d0\u4e2a\u4efb\u52a1\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u4e2d\u7684<code>Timer<\/code>\u7c7b\u6765\u5b9e\u73b0\u5b9a\u65f6\u5668\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def task():<\/p>\n<p>    print(&quot;\u6267\u884c\u4efb\u52a1&quot;)<\/p>\n<p>    # \u518d\u6b21\u8bbe\u7f6e\u5b9a\u65f6\u5668<\/p>\n<p>    threading.Timer(5, task).start()<\/p>\n<h2><strong>\u521d\u59cb\u542f\u52a8\u5b9a\u65f6\u5668<\/strong><\/h2>\n<p>threading.Timer(5, task).start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u96945\u79d2\u949f\uff0c\u5b9a\u65f6\u5668\u90fd\u4f1a\u8c03\u7528\u4e00\u6b21<code>task<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b88\u62a4\u8fdb\u7a0b<\/h3>\n<\/p>\n<p><p>\u5b88\u62a4\u8fdb\u7a0b\u662f\u4e00\u79cd\u5728\u540e\u53f0\u8fd0\u884c\u7684\u8fdb\u7a0b\uff0c\u901a\u5e38\u7528\u4e8e\u6267\u884c\u957f\u671f\u8fd0\u884c\u7684\u4efb\u52a1\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>daemon<\/code>\u53c2\u6570\u6765\u521b\u5efa\u5b88\u62a4\u8fdb\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def task():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u6267\u884c\u4efb\u52a1&quot;)<\/p>\n<p>        time.sleep(5)<\/p>\n<h2><strong>\u521b\u5efa\u5b88\u62a4\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=task)<\/p>\n<p>thread.daemon = True<\/p>\n<p>thread.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    print(&quot;\u4e3b\u7ebf\u7a0b\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>    time.sleep(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>task<\/code>\u51fd\u6570\u4f5c\u4e3a\u5b88\u62a4\u7ebf\u7a0b\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff0c\u800c\u4e3b\u7ebf\u7a0b\u5219\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6<\/h3>\n<\/p>\n<p><p>\u4efb\u52a1\u8c03\u5ea6\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u70b9\u6216\u65f6\u95f4\u95f4\u9694\u5185\u6267\u884c\u4efb\u52a1\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>schedule<\/code>\u5e93\u6765\u5b9e\u73b0\u4efb\u52a1\u8c03\u5ea6\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import schedule<\/p>\n<p>import time<\/p>\n<p>def task():<\/p>\n<p>    print(&quot;\u6267\u884c\u4efb\u52a1&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4efb\u52a1\u8c03\u5ea6<\/strong><\/h2>\n<p>schedule.every(5).seconds.do(task)<\/p>\n<p>while True:<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>schedule<\/code>\u5e93\u4f1a\u6bcf\u96945\u79d2\u949f\u8c03\u7528\u4e00\u6b21<code>task<\/code>\u51fd\u6570\uff0c\u4ece\u800c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b<\/h3>\n<\/p>\n<p><p>\u591a\u7ebf\u7a0b\u6280\u672f\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>threading<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u591a\u7ebf\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import time<\/p>\n<p>def task1():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u6267\u884c\u4efb\u52a11&quot;)<\/p>\n<p>        time.sleep(5)<\/p>\n<p>def task2():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u6267\u884c\u4efb\u52a12&quot;)<\/p>\n<p>        time.sleep(3)<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1 = threading.Thread(target=task1)<\/p>\n<p>thread2 = threading.Thread(target=task2)<\/p>\n<h2><strong>\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread1.start()<\/p>\n<p>thread2.start()<\/p>\n<h2><strong>\u4e3b\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    print(&quot;\u4e3b\u7ebf\u7a0b\u6b63\u5728\u8fd0\u884c&quot;)<\/p>\n<p>    time.sleep(10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>task1<\/code>\u548c<code>task2<\/code>\u51fd\u6570\u4f5c\u4e3a\u4e24\u4e2a\u72ec\u7acb\u7684\u7ebf\u7a0b\u540c\u65f6\u8fd0\u884c\uff0c\u800c\u4e3b\u7ebf\u7a0b\u5219\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5f02\u6b65\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>\u5f02\u6b65\u7f16\u7a0b\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u7b49\u5f85I\/O\u64cd\u4f5c\u65f6\u7ee7\u7eed\u6267\u884c\u5176\u4ed6\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u6548\u7387\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>asyncio<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u5f02\u6b65\u7f16\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def task1():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u6267\u884c\u4efb\u52a11&quot;)<\/p>\n<p>        aw<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>t asyncio.sleep(5)<\/p>\n<p>async def task2():<\/p>\n<p>    while True:<\/p>\n<p>        print(&quot;\u6267\u884c\u4efb\u52a12&quot;)<\/p>\n<p>        await asyncio.sleep(3)<\/p>\n<p>async def main():<\/p>\n<p>    await asyncio.gather(task1(), task2())<\/p>\n<h2><strong>\u8fd0\u884c\u5f02\u6b65\u4efb\u52a1<\/strong><\/h2>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>task1<\/code>\u548c<code>task2<\/code>\u51fd\u6570\u4f5c\u4e3a\u5f02\u6b65\u4efb\u52a1\u540c\u65f6\u8fd0\u884c\uff0c\u800c<code>main<\/code>\u51fd\u6570\u8d1f\u8d23\u542f\u52a8\u8fd9\u4e9b\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u7cfb\u7edf\u670d\u52a1<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u957f\u671f\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7cfb\u7edf\u670d\u52a1\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>systemd<\/code>\u6765\u521b\u5efa\u548c\u7ba1\u7406\u7cfb\u7edf\u670d\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u670d\u52a1\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">sudo nano \/etc\/systemd\/system\/my_service.service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5728\u670d\u52a1\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-ini\">[Unit]<\/p>\n<p>Description=My Python Service<\/p>\n<p>[Service]<\/p>\n<p>ExecStart=\/usr\/bin\/python3 \/path\/to\/your_script.py<\/p>\n<p>Restart=always<\/p>\n<p>[Install]<\/p>\n<p>WantedBy=multi-user.target<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u542f\u52a8\u548c\u542f\u7528\u670d\u52a1\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">sudo systemctl start my_service.service<\/p>\n<p>sudo systemctl enable my_service.service<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u521b\u5efa\u548c\u914d\u7f6e<code>systemd<\/code>\u670d\u52a1\u6587\u4ef6\uff0c\u53ef\u4ee5\u8ba9Python\u7a0b\u5e8f\u4f5c\u4e3a\u7cfb\u7edf\u670d\u52a1\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u5bb9\u5668\u5316\u6280\u672f<\/h3>\n<\/p>\n<p><p>\u5bb9\u5668\u5316\u6280\u672f\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u9694\u79bb\u7684\u8fd0\u884c\u73af\u5883\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u80fd\u591f\u8de8\u5e73\u53f0\u8fd0\u884c\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Docker\u6765\u5b9e\u73b0\u5bb9\u5668\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efaDockerfile\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-Dockerfile\">FROM python:3.8-slim<\/p>\n<p>COPY your_script.py \/app\/your_script.py<\/p>\n<p>CMD [&quot;python&quot;, &quot;\/app\/your_script.py&quot;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6784\u5efaDocker\u955c\u50cf\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">docker build -t my_python_app .<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u8fd0\u884cDocker\u5bb9\u5668\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">docker run -d --name my_python_app_container my_python_app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u521b\u5efa\u548c\u8fd0\u884cDocker\u5bb9\u5668\uff0c\u53ef\u4ee5\u8ba9Python\u7a0b\u5e8f\u5728\u9694\u79bb\u7684\u73af\u5883\u4e2d\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528\u6d88\u606f\u961f\u5217<\/h3>\n<\/p>\n<p><p>\u6d88\u606f\u961f\u5217\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\u6d88\u606f\u7684\u6570\u636e\u7ed3\u6784\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>RabbitMQ<\/code>\u3001<code>Kafka<\/code>\u7b49\u6d88\u606f\u961f\u5217\u5de5\u5177\u6765\u5b9e\u73b0\u4efb\u52a1\u7684\u5f02\u6b65\u5904\u7406\u548c\u6301\u7eed\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>RabbitMQ<\/code>\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5<code>pika<\/code>\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install pika<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u751f\u4ea7\u8005\u811a\u672c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>channel = connection.channel()<\/p>\n<p>channel.queue_declare(queue=&#39;task_queue&#39;, durable=True)<\/p>\n<p>def send_task(task):<\/p>\n<p>    channel.basic_publish(<\/p>\n<p>        exchange=&#39;&#39;,<\/p>\n<p>        routing_key=&#39;task_queue&#39;,<\/p>\n<p>        body=task,<\/p>\n<p>        properties=pika.BasicProperties(<\/p>\n<p>            delivery_mode=2,  # make message persistent<\/p>\n<p>        ))<\/p>\n<p>    print(f&quot; [x] Sent {task}&quot;)<\/p>\n<p>for i in range(10):<\/p>\n<p>    send_task(f&quot;Task {i}&quot;)<\/p>\n<p>connection.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u521b\u5efa\u6d88\u8d39\u8005\u811a\u672c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pika<\/p>\n<p>import time<\/p>\n<p>connection = pika.BlockingConnection(pika.ConnectionParameters(&#39;localhost&#39;))<\/p>\n<p>channel = connection.channel()<\/p>\n<p>channel.queue_declare(queue=&#39;task_queue&#39;, durable=True)<\/p>\n<p>def callback(ch, method, properties, body):<\/p>\n<p>    print(f&quot; [x] Received {body}&quot;)<\/p>\n<p>    time.sleep(5)<\/p>\n<p>    print(&quot; [x] Done&quot;)<\/p>\n<p>    ch.basic_ack(delivery_tag=method.delivery_tag)<\/p>\n<p>channel.basic_qos(prefetch_count=1)<\/p>\n<p>channel.basic_consume(queue=&#39;task_queue&#39;, on_message_callback=callback)<\/p>\n<p>print(&#39; [*] Waiting for messages. To exit press CTRL+C&#39;)<\/p>\n<p>channel.start_consuming()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528<code>RabbitMQ<\/code>\u6d88\u606f\u961f\u5217\uff0c\u53ef\u4ee5\u8ba9\u4efb\u52a1\u5728\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u9012\uff0c\u4ece\u800c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528\u5b9a\u65f6\u4efb\u52a1<\/h3>\n<\/p>\n<p><p>\u5b9a\u65f6\u4efb\u52a1\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5728\u7279\u5b9a\u7684\u65f6\u95f4\u70b9\u6267\u884c\u4efb\u52a1\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>APScheduler<\/code>\u5e93\u6765\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5<code>APScheduler<\/code>\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install apscheduler<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u5b9a\u65f6\u4efb\u52a1\u811a\u672c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from apscheduler.schedulers.blocking import BlockingScheduler<\/p>\n<p>def task():<\/p>\n<p>    print(&quot;\u6267\u884c\u4efb\u52a1&quot;)<\/p>\n<p>scheduler = BlockingScheduler()<\/p>\n<p>scheduler.add_job(task, &#39;interval&#39;, seconds=5)<\/p>\n<p>print(&#39;Press Ctrl+C to exit&#39;)<\/p>\n<p>try:<\/p>\n<p>    scheduler.start()<\/p>\n<p>except (KeyboardInterrupt, SystemExit):<\/p>\n<p>    pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528<code>APScheduler<\/code>\u5e93\uff0c\u53ef\u4ee5\u8ba9\u5b9a\u65f6\u4efb\u52a1\u6bcf\u96945\u79d2\u949f\u6267\u884c\u4e00\u6b21\uff0c\u4ece\u800c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528\u8fdc\u7a0b\u670d\u52a1\u5668<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u957f\u671f\u8fd0\u884c\u7684\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5c06\u5176\u90e8\u7f72\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>paramiko<\/code>\u5e93\u6765\u8fde\u63a5\u548c\u7ba1\u7406\u8fdc\u7a0b\u670d\u52a1\u5668\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5<code>paramiko<\/code>\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install paramiko<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u8fdc\u7a0b\u8fde\u63a5\u811a\u672c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import paramiko<\/p>\n<p>hostname = &#39;your_server_ip&#39;<\/p>\n<p>username = &#39;your_username&#39;<\/p>\n<p>password = &#39;your_password&#39;<\/p>\n<p>command = &#39;python3 \/path\/to\/your_script.py&#39;<\/p>\n<p>client = paramiko.SSHClient()<\/p>\n<p>client.set_missing_host_key_policy(paramiko.AutoAddPolicy())<\/p>\n<p>client.connect(hostname, username=username, password=password)<\/p>\n<p>stdin, stdout, stderr = client.exec_command(command)<\/p>\n<p>print(stdout.read().decode())<\/p>\n<p>client.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528<code>paramiko<\/code>\u5e93\uff0c\u53ef\u4ee5\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u5e76\u6267\u884cPython\u811a\u672c\uff0c\u4ece\u800c\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528\u4e91\u670d\u52a1<\/h3>\n<\/p>\n<p><p>\u4e91\u670d\u52a1\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u90e8\u7f72\u548c\u7ba1\u7406\u957f\u671f\u8fd0\u884c\u7684\u7a0b\u5e8f\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>AWS Lambda<\/code>\u3001<code>Google Cloud Functions<\/code>\u7b49\u4e91\u670d\u52a1\u6765\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>AWS Lambda<\/code>\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efaLambda\u51fd\u6570\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>def lambda_handler(event, context):<\/p>\n<p>    print(&quot;\u6267\u884c\u4efb\u52a1&quot;)<\/p>\n<p>    return {<\/p>\n<p>        &#39;statusCode&#39;: 200,<\/p>\n<p>        &#39;body&#39;: json.dumps(&#39;Hello from Lambda!&#39;)<\/p>\n<p>    }<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u90e8\u7f72Lambda\u51fd\u6570\uff1a<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528AWS\u7ba1\u7406\u63a7\u5236\u53f0\u6216<code>AWS CLI<\/code>\u5de5\u5177\u5c06Lambda\u51fd\u6570\u90e8\u7f72\u5230AWS\u4e91\u670d\u52a1\u4e0a\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528<code>AWS Lambda<\/code>\uff0c\u53ef\u4ee5\u8ba9Python\u7a0b\u5e8f\u5728\u4e91\u7aef\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u4ecb\u7ecd\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3001\u5b9a\u65f6\u5668\u3001\u5b88\u62a4\u8fdb\u7a0b\u3001\u4efb\u52a1\u8c03\u5ea6\u3001\u591a\u7ebf\u7a0b\u3001\u5f02\u6b65\u7f16\u7a0b\u3001\u7cfb\u7edf\u670d\u52a1\u3001\u5bb9\u5668\u5316\u6280\u672f\u3001\u6d88\u606f\u961f\u5217\u3001\u5b9a\u65f6\u4efb\u52a1\u3001\u8fdc\u7a0b\u670d\u52a1\u5668\u8fd8\u662f\u4e91\u670d\u52a1\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b9e\u73b0Python\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4fdd\u6301Python\u7a0b\u5e8f\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f7fPython\u7a0b\u5e8f\u5728\u540e\u53f0\u6301\u7eed\u8fd0\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u51e0\u79cd\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u7a0b\u5e8f\u4f5c\u4e3a\u7cfb\u7edf\u670d\u52a1\u8fd0\u884c\uff0c\u6216\u8005\u4f7f\u7528nohup\u547d\u4ee4\u542f\u52a8\u7a0b\u5e8f\uff0c\u8fd9\u6837\u5373\u4f7f\u5173\u95ed\u7ec8\u7aef\uff0c\u7a0b\u5e8f\u4ecd\u4f1a\u7ee7\u7eed\u6267\u884c\u3002\u6b64\u5916\uff0c\u5229\u7528\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\u5982supervisord\u6216systemd\uff0c\u4e5f\u80fd\u6709\u6548\u5730\u7ba1\u7406\u548c\u4fdd\u6301\u7a0b\u5e8f\u7684\u6301\u7eed\u8fd0\u884c\u3002<\/p>\n<p><strong>Python\u7a0b\u5e8f\u5d29\u6e83\u540e\u5982\u4f55\u81ea\u52a8\u91cd\u542f\uff1f<\/strong><br \/>\u8981\u786e\u4fddPython\u7a0b\u5e8f\u5728\u5d29\u6e83\u540e\u81ea\u52a8\u91cd\u542f\uff0c\u53ef\u4ee5\u4f7f\u7528try-except\u8bed\u53e5\u6355\u83b7\u5f02\u5e38\uff0c\u5e76\u5728\u6355\u83b7\u5230\u5f02\u5e38\u65f6\u91cd\u65b0\u8c03\u7528\u4e3b\u7a0b\u5e8f\u903b\u8f91\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u8fdb\u7a0b\u7ba1\u7406\u5de5\u5177\u5982supervisord\uff0c\u5b83\u53ef\u4ee5\u76d1\u63a7\u7a0b\u5e8f\u72b6\u6001\u5e76\u5728\u7a0b\u5e8f\u5d29\u6e83\u65f6\u81ea\u52a8\u91cd\u542f\u3002<\/p>\n<p><strong>\u5728\u4e91\u73af\u5883\u4e2d\u5982\u4f55\u786e\u4fddPython\u7a0b\u5e8f\u6301\u7eed\u8fd0\u884c\uff1f<\/strong><br \/>\u5728\u4e91\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u4e91\u670d\u52a1\u63d0\u4f9b\u7684\u8ba1\u7b97\u8d44\u6e90\uff0c\u4f8b\u5982AWS Lambda\u3001Google Cloud Functions\u7b49\uff0c\u6765\u90e8\u7f72Python\u7a0b\u5e8f\u3002\u8fd9\u4e9b\u670d\u52a1\u53ef\u4ee5\u81ea\u52a8\u6269\u5c55\u5e76\u786e\u4fdd\u7a0b\u5e8f\u5728\u9700\u8981\u65f6\u6301\u7eed\u8fd0\u884c\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5bb9\u5668\u5316\u6280\u672f\uff0c\u5982Docker\uff0c\u7ed3\u5408Kubernetes\u8fdb\u884c\u7f16\u6392\uff0c\u4e5f\u80fd\u6709\u6548\u7ba1\u7406\u548c\u8fd0\u884cPython\u7a0b\u5e8f\uff0c\u786e\u4fdd\u5176\u7a33\u5b9a\u6027\u548c\u6301\u7eed\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7fPython\u7a0b\u5e8f\u4e00\u76f4\u8fd0\u884c\u7684\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u65e0\u9650\u5faa\u73af\u3001\u4f7f\u7528\u5b9a\u65f6\u5668\u3001\u4f7f\u7528\u5b88\u62a4\u8fdb\u7a0b\u3001\u4f7f\u7528\u4efb\u52a1\u8c03\u5ea6\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u65e0\u9650 [&hellip;]","protected":false},"author":3,"featured_media":1083741,"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\/1083734"}],"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=1083734"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083734\/revisions"}],"predecessor-version":[{"id":1083743,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1083734\/revisions\/1083743"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1083741"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1083734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1083734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1083734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}