

{"id":157,"date":"2021-07-05T13:37:45","date_gmt":"2021-07-05T05:37:45","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/python\/?p=157"},"modified":"2021-07-05T13:37:45","modified_gmt":"2021-07-05T05:37:45","slug":"python%e7%9a%84process%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/python\/post-157.html","title":{"rendered":"python\u7684process\u7c7b"},"content":{"rendered":"\n<p>\u591a\u8fdb\u7a0b\uff0c\u662f\u591a\u4efb\u52a1\u7684\u53e6\u4e00\u79cd\u65b9\u5f0f\uff0cpython\u7684multiprocess\u6a21\u5757\u662f\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u6a21\u5757\uff0cProcess\u7c7b\u662f\u5173\u952e\u7c7b<\/p>\n\n\n\n<h2>\u8fdb\u7a0b\u7684\u6982\u5ff5<\/h2>\n\n\n\n<p>\u8fdb\u7a0b\uff0c\u662f\u7a0b\u5e8f\u7684\u4e00\u6b21\u6267\u884c\u8fc7\u7a0b\uff0c\u662f\u64cd\u4f5c\u7cfb\u7edf\u8d44\u6e90\u5206\u914d\u7684\u6700\u5c0f\u5355\u4f4d\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8ewindows\u7cfb\u7edf\uff0c\u53ef\u4ee5\u5728\u4efb\u52a1\u7ba1\u7406\u5668\u4e2d\u67e5\u770b\u5f53\u524d\u8fd0\u884c\u7684\u6240\u6709\u8fdb\u7a0b\u3002linux\u4e00\u822c\u4f7f\u7528ps\u547d\u4ee4\u67e5\u770b<\/p>\n\n\n\n<p>\u5f53\u7a0b\u5e8f\u52a0\u8f7d\u5230\u5185\u5b58\u5f00\u59cb\uff0c\u5230\u4ece\u5185\u5b58\u4e2d\u91ca\u653e\uff0c\u662f\u8fdb\u7a0b\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n\n\n\n<p>\u7a0b\u5e8f\uff0c\u662f\u4e00\u4e2a\u9759\u6001\u6982\u5ff5\uff0c\u6307\u7684\u662f\u7f16\u5199\u597d\u7684\u4ee3\u7801\uff0c\u4fdd\u5b58\u5728\u786c\u76d8\u4e0a\uff0c\u800c\u8fdb\u7a0b\u662f\u52a8\u6001\u7684\uff0c\u5b58\u5728\u5185\u5b58\u4e2d\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5bf9\u5e94\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\uff0c\u4f8b\u5982\u540c\u65f6\u8fd0\u884c\u591a\u4e2aqq\uff0c\u591a\u4e2a\u8fdb\u7a0b\u4e4b\u95f4\u53ef\u80fd\u6709\u7236\u5b50\u5173\u7cfb\u3002\u4f7f\u7528multiprocess\u521b\u5efa\u7684\u591a\u8fdb\u7a0b\u5c31\u5b58\u5728\u7236\u5b50\u5173\u7cfb\uff0c\u4f46\u7236\u8fdb\u7a0b\u53ef\u4ee5\u5148\u6b7b\u4ea1\uff0c\u5b50\u8fdb\u7a0b\u4e0d\u4f1a\u88ab\u6740\u6b7b\u3002\uff08\u6ce8\u610f\u7ebf\u7a0b\u662f\u4e0d\u53ef\u4ee5\u7684\uff09<\/p>\n\n\n\n<p>\u8fdb\u7a0b\u4e0e\u7ebf\u7a0b\u4e4b\u95f4\uff0c\u4e5f\u662f\u4e00\u5bf9\u591a\u7684\u5173\u7cfb\u3002\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4e5f\u53ef\u4ee5\u6709\u591a\u4e2a\u7ebf\u7a0b\uff0c\u7ebf\u7a0b\u662f\u8d44\u6e90\u8c03\u5ea6\uff08cpu\uff09\u7684\u6700\u5c0f\u5355\u4f4d\u3002<\/p>\n\n\n\n<h2>\u591a\u8fdb\u7a0b\u5b9e\u4f8b<\/h2>\n\n\n\n<p>\u5bfc\u5165multiprocess\u6a21\u5757\uff0c\u4f7f\u7528Process\u7c7b\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u8c03\u7528start\u65b9\u6cd5\u542f\u52a8\u8be5\u8fdb\u7a0b<\/p>\n\n\n\n<p>\u591a\u8fdb\u7a0b\u4e0e\u591a\u7ebf\u7a0b\u4ee3\u7801\u5f88\u76f8\u4f3c\uff0c\u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6216\u7ee7\u627fprocess\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import multiprocessing\nimport time\n\n\ndef test1():\n    while True:\n        print(\"test1------\" + str(1))\n        time.sleep(1)\n\n\ndef test2():\n    while True:\n        print(\"test2---\" + str(2))\n        time.sleep(1)\n\n\ndef main():\n    t1 = multiprocessing.Process(target=test1, )\n    t2 = multiprocessing.Process(target=test2, )\n\n    t1.start()\n    t2.start()\n\n    while True:\n        print(\"main--\" + str(3))\n        time.sleep(1)\n\n\nif __name__ == \"__main__\":\n    main()\n<\/code><\/pre>\n\n\n\n<p>\u6740\u6b7b\u5176\u4e2d\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5176\u4ed6\u8fdb\u7a0b\u4e0d\u4f1a\u53d7\u5f71\u54cd\uff0c\u6740\u6b7b\u7236\u8fdb\u7a0b\u540e\uff0c\u5b50\u8fdb\u7a0b\u53d8\u6210\u5b64\u513f\u8fdb\u7a0b\uff0c\u4f1a\u88ab\u64cd\u4f5c\u7cfb\u7edf\u6258\u7ba1\u800c\u4e0d\u88ab\u7ec8\u6b62\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u7ebf\u7a0b\uff0c\u8fdb\u7a0b\uff0c\u8fd8\u6709\u534f\u7a0b\uff0c\u5728\u540e\u7eed\u7684\u7ae0\u8282\u4e2d\u4f1a\u4e86\u89e3\u5230\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u8fdb\u7a0b\uff0c\u662f\u591a\u4efb\u52a1\u7684\u53e6\u4e00\u79cd\u65b9\u5f0f\uff0cpython\u7684multiprocess\u6a21\u5757\u662f\u5b9e\u73b0\u591a\u7ebf\u7a0b\u7684\u6a21\u5757\uff0cProcess\u7c7b\u662f&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/python\/post-157.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">python\u7684process\u7c7b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/157"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}