{"id":203,"date":"2023-03-14T17:53:48","date_gmt":"2023-03-14T09:53:48","guid":{"rendered":"https:\/\/www.road-trip.cc\/?p=203"},"modified":"2023-03-14T17:53:48","modified_gmt":"2023-03-14T09:53:48","slug":"uwsig-prefork-vs-lazy-apps","status":"publish","type":"post","link":"https:\/\/www.road-trip.cc\/?p=203","title":{"rendered":"uwsig prefork vs lazy-apps"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u95ee\u9898\u80cc\u666f<\/h2>\n\n\n\n<p>\u5728\u5de5\u4f5c\u4e2d\uff0c\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u4f7f\u7528uwsgi+flask \u8fd9\u5bf9\u642d\u6863\u65f6\uff0c\u5f15\u5165\u4e86apollo\u52a8\u6001\u914d\u7f6e\u7ba1\u7406\u670d\u52a1\uff0c\u5728\u542f\u52a8uwsgi\u7684\u8fc7\u7a0b\u4e2d\u4f1a\u53bb\u94fe\u63a5apollo\u670d\u52a1\u5668\uff08\u5982\u4ee3\u7801\u6240\u793a\uff09\uff0c\u8fd9\u65f6\u5019\u4f1a\u53d1\u73b0\u867d\u7136\u770b\u4e0a\u53bbuwsgi\u542f\u52a8\u6210\u529f\u4e86\uff0c\u4f46\u662f\u5374\u4e0d\u80fd\u63d0\u4f9b\u670d\u52a1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import json\nimport time\nfrom flask import Flask\nfrom doom.utils.apollo import apollo_get_value\nfrom doom.utils.apollo import configure_apollo\n\n\ndef create_app():\n    app = Flask(__name__)\n    app.config&#91;'APOLLO_APPID'] = \"apolllo_appid\"\n    app.config&#91;'APOLLO_NAMESPACE'] = \"ns.python.json\"\n    app.config&#91;'APOLLO_CONFIG_SERVER_URL'] = \"http:\/\/apollo_server.com:7088\"\n    configure_apollo(app)\n\n    # time.sleep(1)\n\n    @app.route(\"\/\", methods=&#91;'GET'])\n    def index():\n        print(\"-\" * 20)\n        print(json.dumps(apollo_get_value(app), ensure_ascii=False, indent=4))\n        print(\"+\" * 20)\n        return \"pong\\n\"\n\n    return app\n\n\napp = create_app()\n\n\nif __name__ == \"__main__\":\n    app.run()\n\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>uwsgi --http-socket 0.0.0.0:5001 --process 4 --threads 4 --plugin python3 --wsgi-file manage.py --callable app --stats 0.0.0.0:9191<\/code><\/pre>\n\n\n\n<p>\u5982\u4ee3\u7801\u548c\u542f\u52a8\u65b9\u5f0f\u6240\u793a\uff0cuwsgi\u542f\u52a8\u540e\uff0c\u4f7f\u7528curl\u6d4b\u8bd5\u65f6\uff0c\u4f1ahang\u4f4f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5206\u6790\u8bb0\u5f55<\/h2>\n\n\n\n<p>uwsgi\u5728\u542f\u52a8\u540e\uff0c\u5bb6\u5728python\u4ee3\u7801\u540e\uff0c\u4f1afork\u51fa\u6765\u5176\u4ed6\u7684worker\u8fdb\u7a0b\uff08\u8fd9\u6837\u8fdb\u52a0\u8f7d\u4e00\u6b21\u4ee3\u7801\u5373\u53ef\uff09\uff0c\uff08\u4e00\u4e0b\u5206\u6790\u4e0d\u786e\u5b9a\u662f\u5426\u6b63\u786e\uff09\uff0c\u731c\u6d4b\u5728fork\u65f6\uff0c\u5c1a\u672a\u94fe\u63a5\u81f3apollo\uff0c\u5bfc\u81f4fork\u51fa\u6765\u7684worker\u4e0d\u80fd\u548capollo\u6b63\u786e\u4ea4\u4e92\u3002\uff08\u9700\u8981\u8be6\u7ec6\u5206\u6790\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u529e\u6cd5<\/h2>\n\n\n\n<p>\u4f7f\u7528uwsgi\u7684lazy-apps\u6a21\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uwsgi --http-socket 0.0.0.0:5001 --process 4 --threads 4 --plugin python3 --wsgi-file manage.py --callable app --stats 0.0.0.0:9191 --lazy-apps<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u6458\u5f55\u6587\u6863<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<h2 class=\"wp-block-heading\">Lazy apps VS&nbsp;<strong>prefork<\/strong><a href=\"https:\/\/uwsgi-docs.readthedocs.io\/en\/latest\/articles\/MassiveHostingWithEmperorAndNamespaces.html?highlight=prefork#lazy-apps-vs-prefork\"><\/a><\/h2>\n\n\n\n<p>One of the controversial design choices of uWSGI is \u201c<strong>prefork<\/strong>ing by default\u201d.<\/p>\n\n\n\n<p>It means your app is loaded on startup and then fork() is called for each worker.<\/p>\n\n\n\n<p>While this is the common approach in the UNIX world and it is an expected behaviour for a Perl developer (that is historically more near to the UNIX world) it is totally unknown and unexpected by a Python (and maybe Ruby) one.<\/p>\n\n\n\n<p>So one of the choices you need to make when building a uWSGI-based service is how to manage the fork() behaviour.<\/p>\n\n\n\n<p>If you are unsure let me tell you one thing: with&nbsp;<strong>prefork<\/strong>ing behaviour you will make some user very happy, and lot of users completely lost. With \u2013lazy-apps you will have all of your users totally unconcerned. Trust me, few happy users cannot make you happy too when you have angry customers too.<\/p>\n\n\n\n<p>So, uWSGI default fork() behaviour is generally wrong for massive hosting, so add \u2013lazy-apps and eventually give the advanced users the freedom to change it when needed.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/uwsgi-docs.readthedocs.io\/en\/latest\/articles\/MassiveHostingWithEmperorAndNamespaces.html?highlight=prefork#lazy-apps-vs-prefork\">Massive \u201csecure\u201d Hosting with the Emperor and Linux Namespaces, AKA \u201cImproving unbit.it and pythonanywhere.com\u201d \u2014 uWSGI 2.0 documentation (uwsgi-docs.readthedocs.io)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/apolloconfig\/apollo\">GitHub &#8211; apolloconfig\/apollo: Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u80cc\u666f \u5728\u5de5\u4f5c\u4e2d\uff0c\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u4f7f\u7528uwsgi+flask \u8fd9\u5bf9\u642d\u6863\u65f6\uff0c\u5f15\u5165\u4e86apollo\u52a8\u6001\u914d\u7f6e\u7ba1\u7406\u670d\u52a1\uff0c &#8230; <a title=\"uwsig prefork vs lazy-apps\" class=\"read-more\" href=\"https:\/\/www.road-trip.cc\/?p=203\" aria-label=\"\u9605\u8bfb uwsig prefork vs lazy-apps\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=203"}],"version-history":[{"count":3,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=\/wp\/v2\/posts\/203\/revisions\/206"}],"wp:attachment":[{"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.road-trip.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}