Flask 2.0 async — Ошибка времени выполнения: Работа вне контекста запроса

#flask #python-asyncio #flask-restful

Вопрос:

Получение этого при доступе get_features к api POST

 RuntimeError: Working outside of request context
This typically means that you attempted to use functionality that needed
an active HTTP request.  Consult the documentation on testing for
information about how to avoid this problem.
 
 @api_v1.route('/get_features', methods=['POST'])
async def get_features():
    results = []
    with app.app_context():
        inputs = request.json
        responses = await get_features_for_all(inputs)
        for response in responses:
            results.append(response)
        return jsonify({'result': results})
 

Ответ №1:

Попробуйте обойтись без with app.app_context(): строки, так как вы уже находитесь в контексте (маршрут колбы).