本文将为您提供关于散布flask模型时,引发RuntimeError:“未在数据库上注册应用程序”的详细介绍,同时,我们还将为您提供关于AsyncioRuntimeError:事件循环已关闭、djan
本文将为您提供关于散布flask模型时,引发RuntimeError:“未在数据库上注册应用程序”的详细介绍,同时,我们还将为您提供关于Asyncio RuntimeError:事件循环已关闭、django – RuntimeError:应用程序“目录”中的’product_product_options’模型冲突、Flask RuntimeError:没有设置图像的目的地、flask 流式响应 RuntimeError: working outside of request context的实用信息。
本文目录一览:- 散布flask模型时,引发RuntimeError:“未在数据库上注册应用程序”
- Asyncio RuntimeError:事件循环已关闭
- django – RuntimeError:应用程序“目录”中的’product_product_options’模型冲突
- Flask RuntimeError:没有设置图像的目的地
- flask 流式响应 RuntimeError: working outside of request context
散布flask模型时,引发RuntimeError:“未在数据库上注册应用程序”
我通过分散模型,蓝图来重构我的Flask应用程序,但是我遇到了运行时错误。
def create_app(): app = flask.Flask("app") app.config[''SQLALCHEMY_DATABASE_URI''] = ''sqlite://'' app.register_blueprint(api) db.init_app(app) db.create_all() return app
我有以下问题(示例项目托管在这里:https : //github.com/chfw/sample):
Traceback (most recent call last): File "application.py", line 17, in <module> app = create_app() File "application.py", line 12, in create_app db.create_all() File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 856, in create_all self._execute_for_all_tables(app, bind, ''create_all'') File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 836, in _execute_for_all_tables app = self.get_app(app) File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 809, in get_app raise RuntimeError(''application not registered on db ''RuntimeError: application not registered on db instance and no application bound to current context
答案1
小编典典这与Flask的应用程序上下文有关。当使用初始化时db.init_app(app)
,Flask-SQLAlchemy不知道哪个应用程序是“当前”应用程序(请记住,Flask允许在同一解释器中使用多个应用程序)。你可能有多个应用程序SQLAlchemy在同一进程中使用同一实例,并且Flask-SQLAlchemy将需要知道哪个是“当前”应用程序(由于Flask的上下文上下文是所有事物的本地性质)。
如果你需要在运行时执行此操作,则必须明确说明哪个应用程序是所有调用的“当前”应用程序。你可以通过将代码更改为使用with app.app_context()
块来做到这一点:
def create_app(): app = flask.Flask("app") app.config[''SQLALCHEMY_DATABASE_URI''] = ''sqlite://'' app.register_blueprint(api) db.init_app(app) with app.app_context(): # Extensions like Flask-SQLAlchemy now know what the "current" app # is while within this block. Therefore, you can now run........ db.create_all() return app
如果要编写需要应用程序上下文的独立脚本,则可以在开始时推送上下文,而不是将所有内容放在一个with块中。
create_app().app_context().push()
如果你为Flask的cli编写命令,则该命令将自动访问上下文。
Asyncio RuntimeError:事件循环已关闭
我正在尝试使用Asyncio和aiohttp库发出一堆请求(〜1000),但是遇到一个我找不到太多信息的问题。
当我使用10个网址运行此代码时,它运行得很好。当我用100多个网址运行它时,它中断并给我RuntimeError: Event loop is
closed
错误。
import asyncio
import aiohttp
@asyncio.coroutine
def get_status(url):
code = '000'
try:
res = yield from asyncio.wait_for(aiohttp.request('GET',url),4)
code = res.status
res.close()
except Exception as e:
print(e)
print(code)
if __name__ == "__main__":
urls = ['https://google.com/'] * 100
coros = [asyncio.Task(get_status(url)) for url in urls]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(coros))
loop.close()
堆栈跟踪可以在这里找到。
我已经努力奋斗了几个小时,对您的帮助或见解将不胜感激。显然,这表明已经关闭了一个事件循环,但该循环仍然应该打开,但是我不知道这怎么可能。
django – RuntimeError:应用程序“目录”中的’product_product_options’模型冲突
Python 3.4,Django 1.8,Oscar Commerce – VERSION =(1,2,1,’final’)
我试图在documentation之后的目录应用程序中自定义产品.
分叉了目录应用程序,我已经定义了models.py如下:
from django.db import models from oscar.apps.catalogue.abstract_models import AbstractProduct class Product(AbstractProduct): is_active = models.BooleanField(default=False) from oscar.apps.catalogue.models import *
我已经将修改后的目录包含在settings.py中的INSTALLED_APPS中作为列表,如同类似问题here所建议的那样.
INSTALLED_APPS = INSTALLED_APPS + get_core_apps( [''app.gravytrain.catalogue'',])
已将迁移文件夹从oscar / apps / catalog复制到我的自定义应用程序.
但是,运行迁移会导致以下错误:
RuntimeError: Conflicting ''product_product_options'' models in application ''catalogue'': <class ''gravytrain.catalogue.models.Product_product_options''> and <class app.gravytrain.catalogue.models.Product_product_options''>.
我该如何克服这个错误?
解决方法
例如:
from oscar.core.loading import get_model Product = get_model(''catalogue'',''Product'')
Flask RuntimeError:没有设置图像的目的地
错误消息明确指出您尚未配置上传目标。
你可以看看这个错误是如何发生的源代码。
https://github.com/jugmac00/flask-reuploaded/blob/80654e1c9909025c894cddff940cccfbe9c042d7/src/flask_uploads/flask_uploads.py#L73-L84
当我从上面看你的代码时......你定义了一个配置类,但你没有使用它,你还配置了许多与 Flask-Uploads
相关的选项,但你把它们注释掉了
=> Flask-Uploads,尤其是未配置目的地
flask 流式响应 RuntimeError: working outside of request context
1、问题
最近要实现这样一个功能:某个 cgi 处理会很耗时,需要把处理的结果实时的反馈给前端,而不能等到后台全完成了再咔一下全扔前端,那样的用户体验谁都没法接受。
web 框架选的 flask,这个比较轻量级,看了下官方文档,恰好有个叫 Streaming from Templates 的功能:
http://flask.pocoo.org/docs/patterns/streaming/#streaming-from-templates
可以满足需求,它以 generate yield 为基础,流式的返回数据到前端。看了下官方的例子貌似很简单,一笔带过,我又搜了下 stackoverflow,上面有个老外给了个更加详尽的例子:Streaming data with Python and Flask
http://stackoverflow.com/questions/13386681/streaming-data-with-python-and-flask
文中的答案没有前后端的数据交互过程,那我就根据自己的需求加个 http 的交互过程了:
@app.route(''/username'', methods=[''GET'', ''POST''])
def index():
req =request
print req
print "111------------" + req.method + "\n"
def ggg1(req):
print req # the req not my pass into the req....
print "444------------" + req.method + "\n"
if req.method == ''POST'':
if request.form[''username'']:
urlList = request.form[''username''].splitlines()
i = 0
for url in urlList():
i += 1
resultStr = url
print i, resultStr
yield i, resultStr
print req
print "222------------" + req.method + "\n"
return Response(stream_template(''index.html'', data=ggg1(req)))
好吧,这么一加,噩梦就开始了。。。奇葩的问题出现了:
要么第 5 行和第 8 行不等,要么就是第 9 行报错:
if request.method == ''POST'': # RuntimeError: working outside of request context
继续在 stackoverflow 上搜索,发现有人遇到了同样的问题,得到的建议是在调用前声明一个 request 上下文:
with app.test_request_context(''/username'', method=''GET''):
index()
折腾了老半天,还是依旧报错:RuntimeError: working outside of request context
看起来似乎是在进入迭代器以前,原本的 request 的生命周期就已经结束了,因此就没办法再调用了。
那么要解决就有 2 种办法了:
(1)在进入 generationFunc 前将请求复制一份保存下来以供 generationFunc 调用。
(2)利用 app.test_request_context 创建的是一个全新的 request,将数据传给 generationFunc 使用。
以上这两种办法都曾试过,但是由于理解上的偏差,导致一直未能成功。后来经过 坚实 同学的指点,才明白个中缘由,问题得以解决。
2、解决方案
(1)复制 request
将请求复制下来但不能直接 req = request 这种形式,这只是给 request 取了个别名,它们是共享引用。正确的代码如下:
from flask.ctx import _request_ctx_stack
global new_request
@app.route(''/'')
@app.route(''/demo'', methods=[''POST''])
def index():
ctx = _request_ctx_stack.top.copy()
new_request = ctx.request
def generateFunc():
if new_request.method == ''POST'':
if new_request.form[''digitValue'']:
num = int(new_request.form[''digitValue''])
i = 0
for n in xrange(num):
i += 1
print "%s:\t%s" % (i, n)
yield i, n
return Response(stream_template(''index.html'', data=generateFunc()))
PS: 其实像 _request_ctx_stack 这种以下划线开头的变量属于私有变量,外部是不应该调用的,不过坚实同学暂时也没有找到其他能正式调用到它的方法 ,就先这么用着吧。
(2)构造全新 request
上面的这种写法:with app.test_request_context(''/username'', method=''GET''):
之所以不可以是因为 app.test_request_context 创建的是一个全新的 request,它包含的 url, method, headers, form 值都是要在创建时自定义的,它不会把原来的 request 里的数据带进来,需要自己传进去,类似这样:
with app.test_request_context(''/demo'', method=''POST'', data=request.form) as new_context:
def generateFunc():
PS: test_request_context 应该是做单元测试用的,用来模仿用户发起的 HTTP 请求。
它做的事,和你通过浏览器提交一个表单或访问某个网页是差不多的。
例如你传给它 url=''xxx''、method=''post'' 等等参数就是告诉它:向 xxx 发起一个 http 请求
(3)关于 @copy_current_request_context
这是官方宣称在 1.0 中实现的一个新特性,http://flask.pocoo.org/docs/api/#flask.copy_current_request_context 看说明应该可以更加优雅的解决上述问题,
但是试了下貌似不行,可能是组件间的兼容性问题。
(4)关于 Streaming with Context
New in version 0.9.
Note that when you stream data, the request context is already gone the moment the function executes. Flask 0.9 provides you with a helper that can keep the request context around during the execution of the generator:
from flask import stream_with_context, request, Response
@app.route(''/stream'')
def streamed_response():
def generate():
yield ''Hello ''
yield request.args[''name'']
yield ''!''
return Response(stream_with_context(generate()))
Without the stream_with_context() function you would get a RuntimeError at that point.
REF:
http://stackoverflow.com/questions/19755557/streaming-data-with-python-and-flask-raise-runtimeerror-working-outside-of-requ/20189866?noredirect=1#20189866
3、结论
(1)flask.request 和 streaming templates 兼容性不是很好,应该尽量不在 streaming templates 里调用 request,
把需要的值提前准备好,然后再传到 templates 里。这里也有人遇到同样的问题:
http://flask.pocoo.org/mailinglist/archive/2012/4/1/jinja2-stream-doesn-t-work/#8afda9ecd9682b16e8198a2f34e336fb
用 copy_current_request_context 没有效果应该也是上面这个原因。
(2)在文档语焉不详,同时 google 不到答案的时候,读源码或许是最后的选择,这也是一种能力吧。。。 - _ -
4、Refer:
http://stackoverflow.com/questions/13386681/streaming-data-with-python-and-flask
http://flask.pocoo.org/docs/patterns/streaming/
http://stackoverflow.com/questions/8224333/scrolling-log-file-tail-f-animation-using-javascript
http://jsfiddle.net/manuel/zejCD/1/
附坚实同学的 github 与 sf 地址:
https://github.com/anjianshi
http://segmentfault.com/u/anjianshi
5、最后附上完整的测试源码:
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding(''utf-8'')
from flask import Flask, request, Response
app = Flask(__name__)
def stream_template(template_name, **context):
# http://flask.pocoo.org/docs/patterns/streaming/#streaming-from-templates
app.update_template_context(context)
t = app.jinja_env.get_template(template_name)
rv = t.stream(context)
# uncomment if you don''t need immediate reaction
##rv.enable_buffering(5)
return rv
@app.route(''/'')
@app.route(''/demo'', methods=[''POST''])
def index():
with app.test_request_context(''/demo'', method=''POST'', data=request.form) as new_context:
def generateFunc():
new_request = new_context.request
if new_request.method == ''POST'':
if new_request.form[''digitValue'']:
num = int(new_request.form[''digitValue''])
i = 0
for n in xrange(num):
i += 1
print "%s:\t%s" % (i, n)
yield i, n
return Response(stream_template(''index.html'', data=generateFunc()))
if __name__ == "__main__":
app.run(host=''localhost'', port=8888, debug=True)
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 101 Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn''t work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<style>
#data {
border: 1px solid blue;
height: 500px;
width: 500px;
overflow: hidden;
}
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function tailScroll() {
var height = $("#data").get(0).scrollHeight;
$("#data").animate({
scrollTop: height
}, 5);
}
</script>
<form role="form" action="/demo" method="POST">
<textarea rows="1" name="digitValue"></textarea>
<button type="submit" >Submit</button>
</form>
<div id="data" >nothing received yet</div>
{% for i, resultStr in data: %}
<script>
$("<div />").text("{{ i }}:\t{{ resultStr }}").appendTo("#data")
tailScroll();
</script>
{% endfor %}
<!-- jQuery (necessary for Bootstrap''s JavaScript plugins) -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/static/dist/js/bootstrap.min.js"></script>
</body>
</html>
6、推荐阅读:
[1] 用Flask实现视频数据流传输
http://python.jobbole.com/80994/
https://github.com/miguelgrinberg/flask-video-streaming
[2] Video Streaming with Flask
http://blog.miguelgrinberg.com/post/video-streaming-with-flask
[3] Flask 的 Context 机制
https://blog.tonyseek.com/post/the-context-mechanism-of-flask/
[4] flask 源码解析:session
http://python.jobbole.com/87450/
关于散布flask模型时,引发RuntimeError:“未在数据库上注册应用程序”的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Asyncio RuntimeError:事件循环已关闭、django – RuntimeError:应用程序“目录”中的’product_product_options’模型冲突、Flask RuntimeError:没有设置图像的目的地、flask 流式响应 RuntimeError: working outside of request context的相关信息,请在本站寻找。
本文标签: