{"id":205133,"date":"2025-05-29T12:40:45","date_gmt":"2025-05-29T04:40:45","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205133\/"},"modified":"2025-05-29T12:40:45","modified_gmt":"2025-05-29T04:40:45","slug":"flask-%e5%ba%94%e7%94%a8%e5%bc%80%e5%8f%91%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e8%8e%b7%e5%8f%96%e5%85%a8%e5%b1%80%e9%85%8d%e7%bd%ae-current_app%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205133\/","title":{"rendered":"Flask \u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u6b63\u786e\u83b7\u53d6\u5168\u5c40\u914d\u7f6e current_app\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Flask \u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u6b63\u786e\u83b7\u53d6\u5168\u5c40\u914d\u7f6e current_app\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aFlask \u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u6b63\u786e\u83b7\u53d6\u5168\u5c40\u914d\u7f6e current_app\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324330876742d4bf4b026.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python flask \u83b7\u53d6\u5168\u5c40\u914d\u7f6e current_app \u9047\u5230\u7684\u96be\u9898<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 flask \u6846\u67b6\u8fdb\u884c\u5e94\u7528\u5f00\u53d1\u65f6\uff0c\u5f00\u53d1\u4eba\u5458\u7ecf\u5e38\u9700\u8981\u83b7\u53d6 flask \u7684\u5168\u5c40\u914d\u7f6e current_app\u3002\u4f46\u5f53\u4e1a\u52a1\u903b\u8f91\u5206\u5e03\u5728\u591a\u4e2a\u6587\u4ef6\u65f6\uff0c\u83b7\u53d6 current_app \u53ef\u80fd\u9047\u5230\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5728\u542f\u52a8\u6587\u4ef6 app.py\u3001\u4e1a\u52a1\u6587\u4ef6 data.py \u548c\u7f13\u5b58\u5e93 cache.py \u7684\u60c5\u51b5\u4e0b\uff0cdata.py \u4e2d\u4f7f\u7528 cache.py \u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0 &#8220;working outside of application context&#8221; \u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a cache.py \u4e2d\u4f7f\u7528\u4e86 current_app \u6765\u521d\u59cb\u5316\u7f13\u5b58\u63d2\u4ef6\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u4e24\u79cd\u65b9\u6848\uff1a<\/p>\n<p><strong>\u65b9\u5f0f 1\uff1a\u5728\u542f\u52a8\u6587\u4ef6\u4e2d\u521d\u59cb\u5316\u7f13\u5b58<\/strong><\/p>\n<p>\u5728 app.py \u4e2d\uff0c\u521d\u59cb\u5316\u7f13\u5b58\u5e76\u901a\u8fc7 import \u66b4\u9732\u7ed9\u5176\u4ed6\u6587\u4ef6\uff1a<\/p>\n<pre>from flask import flask\nfrom flask_caching import cache\n\napp = flask(__name__)\ncache = cache(app, config=app.config['cache'])<\/pre>\n<p><strong>\u65b9\u5f0f 2\uff1a\u5728\u4e1a\u52a1\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/strong><\/p>\n<p>\u5728\u4e1a\u52a1\u6587\u4ef6 data.py \u4e2d\uff0c\u4f7f\u7528 with \u8bed\u53e5\u4f5c\u4e3a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff0c\u786e\u4fdd\u5728\u5e94\u7528\u4e0a\u4e0b\u6587\u4e2d\u6267\u884c\u4ee3\u7801\u5757\uff1a<\/p>\n<pre>from flask import current_app\n\nwith app.app_context():\n    # \u5728\u8fd9\u91cc\u8bbf\u95ee current_app<\/pre>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u5e94\u7528\u67b6\u6784\u548c\u5f00\u53d1\u4eba\u5458\u7684\u504f\u597d\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aFlask \u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u6b63\u786e\u83b7\u53d6\u5168\u5c40\u914d\u7f6e current_app\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flask \u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u6b63\u786e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205133","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205133"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205133\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}