{"id":1035,"date":"2022-06-15T17:21:07","date_gmt":"2022-06-15T09:21:07","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=1035"},"modified":"2022-06-15T17:21:07","modified_gmt":"2022-06-15T09:21:07","slug":"gevent%e5%bb%ba%e7%ab%99","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2022\/06\/15\/gevent%e5%bb%ba%e7%ab%99\/","title":{"rendered":"gevent\u90e8\u7f72python\u7f51\u7ad9"},"content":{"rendered":"\n<p>\u4e0a\u7bc7\u6211\u4eec\u7528flask\u90e8\u7f72\u4e86\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u7f51\u7ad9\uff0c\u4f46\u662f\u5728\u5f00\u53d1\u73af\u5883\u4e0b\uff0c\u4e0d\u7a33\u5b9a\uff0c\u6211\u4eec\u53ef\u4ee5\u7528gevent\u518d\u6765\u90e8\u7f72\u4e00\u4e2a<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5148\u5728\u547d\u4ee4\u884cpip install gevent\u88c5\u597d\uff0c\u7136\u540e<\/p>\n\n\n\n<p>from flask import Flask<br>from gevent import pywsgi<br>app = Flask(<strong>name<\/strong>)<\/p>\n\n\n\n<p>@app.route(&#8216;\/&#8217;)<br>def hello_world():<br>return &#8216;hello world&#8217;<\/p>\n\n\n\n<p>server = pywsgi.WSGIServer((&#8216;0.0.0.0&#8217;,12345),app)<br>server.serve_forever()<\/p>\n\n\n\n<p>if <strong>name<\/strong> == &#8216;<strong>main<\/strong>&#8216;:<br>app.run()<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u8fd0\u884c\u540e\u4f1a\u53d1\u73b0\u4ec0\u4e48\u4fe1\u606f\u90fd\u6ca1\u663e\u793a\uff0c\u4f46\u662f\u7528IP:12345\u6b63\u5e38\u8bbf\u95ee\uff01\u4e0d\u77e5\u9053\u8fd9\u7b97\u4e0d\u7b97\u662f\u7528wsgi \u670d\u52a1\u542f\u52a8\u4e86\uff01<\/p>\n\n\n\n<p>\u53e6\u5916\u4f1a\u53d1\u73b0\u8bfb\u53d6 .flaskenv \u7684\u503c\u662f\u8bfb\u53d6\u4e0d\u4e86\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p>import os<\/p>\n\n\n\n<p>mysql_config = {<br>&#8216;DIALECT&#8217; : os.getenv(&#8216;MYSQL_DIALECT&#8217;),<br>&#8216;DRIVER&#8217; : os.getenv(&#8216;MYSQL_DRIVER&#8217;),<br>&#8216;USERNAME&#8217; : os.getenv(&#8216;MYSQL_USERNAME&#8217;),<br>&#8216;PASSWORD&#8217; : os.getenv(&#8216;MYSQL_PASSWORD&#8217;),<br>&#8216;HOST&#8217; : os.getenv(&#8216;MYSQL_HOST&#8217;),<br>&#8216;PORT&#8217; : os.getenv(&#8216;MYSQL_PORT&#8217;),<br>&#8216;DATABASE&#8217; : os.getenv(&#8216;MYSQL_DATABASE&#8217;)<br>}<br>\u9700\u8981\u52a0\u4e0a\u4ee5\u4e0b\u4ee3\u7801\u624d\u80fd\u8bfb\u53d6<\/p>\n\n\n\n<p>from dotenv import load_dotenv<br>load_dotenv(dotenv_path=&#8217;.flaskenv&#8217;,override=True)<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u7bc7\u6211\u4eec\u7528flask\u90e8\u7f72\u4e86\u4e00\u4e2a\u5f88\u7b80\u5355\u7684\u7f51\u7ad9\uff0c\u4f46\u662f\u5728\u5f00\u53d1\u73af\u5883\u4e0b\uff0c\u4e0d\u7a33\u5b9a\uff0c\u6211\u4eec\u53ef\u4ee5\u7528gevent\u518d\u6765\u90e8\u7f72\u4e00\u4e2a \u53ef\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1035"}],"version-history":[{"count":0,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1035\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}