在完成有关乌龟 ORM 的章节之前,我学习了烧瓶。我尝试将烧瓶和乌龟结合起来,但找不到合适的方法。当我调用异步函数时出现错误:
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()
我的问题是如何在烧瓶应用程序中调用异步数据库操作代码?