{"id":205049,"date":"2025-05-29T17:09:56","date_gmt":"2025-05-29T09:09:56","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205049\/"},"modified":"2025-05-29T17:09:56","modified_gmt":"2025-05-29T09:09:56","slug":"%e5%9c%a8-flask-%e5%ba%94%e7%94%a8%e4%b8%ad%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d-runtimeerror-working-outside-of-application-context-%e9%94%99%e8%af%af%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205049\/","title":{"rendered":"\u5728 Flask \u5e94\u7528\u4e2d\u5982\u4f55\u907f\u514d \\&#8221;RuntimeError: Working outside of application context\\&#8221; \u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5728 Flask \u5e94\u7528\u4e2d\u5982\u4f55\u907f\u514d \\&#8221;RuntimeError: Working outside of application context\\&#8221; \u9519\u8bef\uff1f <\/h1>\n<p>\u5077\u5077\u52aa\u529b\uff0c\u6084\u65e0\u58f0\u606f\u5730\u53d8\u5f3a\uff0c\u7136\u540e\u60ca\u8273\u6240\u6709\u4eba\uff01\u54c8\u54c8\uff0c\u5c0f\u4f19\u4f34\u4eec\u53c8\u6765\u5b66\u4e60\u5566~\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u4e0d\u77e5\u9053\u5927\u5bb6\u5bf9\u5176\u90fd\u6709\u591a\u5c11\u4e86\u89e3\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u4e00\u8d77\u6765\u770b\u4e00\u5427\uff01\u5f53\u7136\uff0c\u975e\u5e38\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u8bc4\u8bba\uff0c\u7ed9\u51fa\u5408\u7406\u7684\u5efa\u8bae\uff0c\u6211\u4eec\u4e00\u8d77\u5b66\u4e60\uff0c\u4e00\u8d77\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241122\/173225147167400f4f47ff7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 flask \u4e2d\u83b7\u53d6\u5168\u5c40\u914d\u7f6e current_app \u7684\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u4f7f\u7528 flask \u6846\u67b6\u65f6\uff0c\u5728\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u4e4b\u5916\u8c03\u7528\u7279\u5b9a\u7684\u51fd\u6570\u6216\u5bf9\u8c61\u53ef\u80fd\u4f1a\u51fa\u73b0 &#8220;runtimeerror: working outside of application context&#8221; \u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a flask \u4f9d\u8d56\u4e8e\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u6765\u8bbf\u95ee\u5168\u5c40\u914d\u7f6e\u548c\u8bf7\u6c42\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5728 flask \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 &#8220;current_app&#8221; \u7684\u6700\u4f73\u65b9\u6cd5\u662f\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u521d\u59cb\u5316\u5b83\u3002\u8fd9\u901a\u5e38\u5728 &#8220;app.py&#8221; \u6587\u4ef6\u4e2d\u5b8c\u6210\u3002<\/p>\n<p><strong>\u5177\u4f53\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p>\u5728 &#8220;app.py&#8221; \u6587\u4ef6\u4e2d\uff0c\u5bfc\u5165 flask \u548c flask-caching\uff1a<\/p>\n<pre>from flask import flask\nfrom flask_caching import cache<\/pre>\n<\/li>\n<li>\n<p>\u521b\u5efa flask \u5e94\u7528\u7a0b\u5e8f\u5b9e\u4f8b\uff1a<\/p>\n<pre>app = flask(__name__)<\/pre>\n<\/li>\n<li>\n<p>\u5c06\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u914d\u7f6e\u4f20\u9012\u7ed9 flask-caching \u5b9e\u4f8b\uff1a<\/p>\n<pre>cache = Cache(app, config=app.config['Cache'])<\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u6309\u7167\u8fd9\u4e9b\u6b65\u9aa4\u8fdb\u884c\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u521d\u59cb\u5316 &#8220;current_app&#8221;\uff0c\u4ece\u800c\u907f\u514d\u5728\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u4e4b\u5916\u4f7f\u7528\u5b83\u65f6\u51fa\u73b0\u9519\u8bef\u3002\u60a8\u53ef\u4ee5\u5728\u9700\u8981\u7684\u5730\u65b9\u5bfc\u5165 &#8220;cache&#8221; \u5bf9\u8c61\uff0c\u800c\u65e0\u9700\u62c5\u5fc3\u51fa\u73b0\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\u9519\u8bef\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5728 Flask \u5e94\u7528\u4e2d\u5982\u4f55\u907f\u514d \\&#8221;RuntimeError: Working outside of application context\\&#8221; \u9519\u8bef\uff1f \u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Flask \u5e94\u7528\u4e2d\u5982\u4f55\u907f\u514d &#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-205049","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205049","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=205049"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205049\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}