在 Flask 应用程序中使用 Tortoise ORM 初始化数据库的正确方法是什么?
What is the right way to initialize database using tortoise orm in flask app?
在完成关于 Tortoise ORM 的章节之前,我学习了 Flask。
我尝试将烧瓶和乌龟结合起来,但找不到合适的方法。
当我调用异步函数时出现错误:
Traceback (most recent call last):
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Vladlen\python\pythonBasics\lesson 19\views\doc_income.py", line 19, in new
run_async(create_doc(request.values['account_id'], request.values['sum']))
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\tortoise\__init__.py", line 634, in run_async
loop = asyncio.get_event_loop()
File "C:\Users\Vladlen\AppData\Local\Programs\Python\Python37\lib\asyncio\events.py", line 644, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-7'.
我的一段调用文档创建的代码:
@doc_income_app.route("new", methods=['GET', 'POST'])
def new():
if request.method == 'GET':
return render_template('doc_income/new.html')
else:
run_async(create_doc(request.values['account_id'], request.values['sum']))
return jsonify(ok=True)
async def create_doc(account_id, sum):
await db.init()
doc = db.DocIncome(
account_id=account_id,
sum=sum
)
await doc.save()
我的问题是如何在 Flask 应用程序中调用异步数据库操作代码?
如何从同步代码调用异步代码?
创建事件循环,并在其中 运行 编写代码。问题是事件循环必须清除,所以任何计划的任务都会让它永远不会结束。 (这样做有风险run_until_complete
)
基本上:
asyncio.get_event_loop().run_until_complete(create_doc(...))
乌龟提供的 run_async 助手也强制清理数据库,所以我不确定这是你想要的。
抱歉,您实际上是从一个不兼容的 colour 调用的,并且是开拓性的,我无法提供 texbook 答案。
我原来的回答:
Flask 是一个同步 Python 框架,它不使用事件循环。
考虑使用 asyncio-based 框架,例如 Quart/aiohttp/starlette,它和 Tortoise-ORM 使用相同的并发方法,它应该可以工作。
在完成关于 Tortoise ORM 的章节之前,我学习了 Flask。 我尝试将烧瓶和乌龟结合起来,但找不到合适的方法。 当我调用异步函数时出现错误:
Traceback (most recent call last):
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\flask\app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\Vladlen\python\pythonBasics\lesson 19\views\doc_income.py", line 19, in new
run_async(create_doc(request.values['account_id'], request.values['sum']))
File "C:\Users\Vladlen\python\pythonBasics\venv\lib\site-packages\tortoise\__init__.py", line 634, in run_async
loop = asyncio.get_event_loop()
File "C:\Users\Vladlen\AppData\Local\Programs\Python\Python37\lib\asyncio\events.py", line 644, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-7'.
我的一段调用文档创建的代码:
@doc_income_app.route("new", methods=['GET', 'POST'])
def new():
if request.method == 'GET':
return render_template('doc_income/new.html')
else:
run_async(create_doc(request.values['account_id'], request.values['sum']))
return jsonify(ok=True)
async def create_doc(account_id, sum):
await db.init()
doc = db.DocIncome(
account_id=account_id,
sum=sum
)
await doc.save()
我的问题是如何在 Flask 应用程序中调用异步数据库操作代码?
如何从同步代码调用异步代码?
创建事件循环,并在其中 运行 编写代码。问题是事件循环必须清除,所以任何计划的任务都会让它永远不会结束。 (这样做有风险run_until_complete
)
基本上:
asyncio.get_event_loop().run_until_complete(create_doc(...))
乌龟提供的 run_async 助手也强制清理数据库,所以我不确定这是你想要的。
抱歉,您实际上是从一个不兼容的 colour 调用的,并且是开拓性的,我无法提供 texbook 答案。
我原来的回答: Flask 是一个同步 Python 框架,它不使用事件循环。 考虑使用 asyncio-based 框架,例如 Quart/aiohttp/starlette,它和 Tortoise-ORM 使用相同的并发方法,它应该可以工作。