'request returning "Endpoint request timed out"

I have deployed a flask app on aws lambda using zappa now the app is running fine on all end points except my main one when i give post request on it it returns { "message": "Endpoint request timed out" }

really need a fix or idea how to over come this i need to call the analysis route, the deployed url is

https://2ixfyfcsik.execute-api.eu-west-2.amazonaws.com/dev

tried increasing my app timeout limit none avail it seems api gateway has 30 second time out so how to by pass that or not how to make my app return results in 30 seconds any help appreciated

from flask import Flask, redirect, url_for, request, jsonify
from flask_cors import CORS
import os,json
from hatesonar import Sonar
from profanityfilter import ProfanityFilter


app = Flask(__name__)
CORS(app)



@app.route('/',methods = ['GET'])
def index():
    return jsonify({"message": "Hello World!"})



@app.route('/test',methods = ['GET'])
def test():
    results=[]
    post="Every Day. Narrated by Patch."
    sonar = Sonar()
    offensiveLanguage = sonar.ping(text=post)
    for item in offensiveLanguage['classes']:
        if (item['class_name']=='hate_speech'):
            if(item['confidence']>=0.9):
                hatesonar_hatespeech=item['coinfidence']
            else:
                hatesonar_hatespeech=0
            results.append(hatesonar_hatespeech)
        else:
            pass
        if (item['class_name']=='offensive_language'):
            if(item['confidence']>=0.9):
                hatesonar_swearing=item['coinfidence']
            else:
                hatesonar_swearing=0
            results.append(hatesonar_swearing)
    return jsonify(results)




@app.route('/offensiveLanguage',methods = ['POST', 'GET'])
def login():
   if request.method == 'POST':
      user = request.form['nm']
      return redirect(url_for('success',name = user))
   else:
      sonar = Sonar()
      text = request.args.get('text')
      print("text", text)
      offensiveLanguage = sonar.ping(text=text)
      print("offensiveLanguage", offensiveLanguage)
      return jsonify(offensiveLanguage)


@app.route('/analysis',methods = ['GET','POST'])
def profanity():
    if request.method == 'POST':
        profanitycount=0
        data = request.get_json()
        posts=[]
        for item in data:
            if ('media' in item):
                for x in item['media']:
                    if(x['mediaType']=='post'):
                        if (x['content']):
                            posts.append(x['content'])
                        else:
                            pass
                    else:
                        pass
            else:
                pass
        flat_list = []
        for sublist in posts:
            for item in sublist:
                flat_list.append(item)          
        for post in flat_list:
            pf = ProfanityFilter()
            swearing = pf.is_profane(post)
            if(swearing=='true'):
                profanitycount = profanitycount + 1
            else:
                profanitycount = profanitycount
            sonar = Sonar()
            offensiveLanguage = sonar.ping(text=post)   
    print("profanity", profanitycount)
    return jsonify(profanitycount)


if __name__ == '__main__':
        app.run()





Solution 1:[1]

if your request is sync then try to increase memory in Basic Setting of lambda. It was worked for me (Your function is allocated CPU proportional to the memory configured).

Solution 2:[2]

You can use multi-threading to decrease the response time.

This link may be helpful for you AWS Lambda And Multi Threading Using Python

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saurabh
Solution 2 Akram Khan