简体   繁体   中英

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()



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).

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

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

Mid-2022 there is now an official solution that bypasses the API Gateway and hence the annoying 29 second limit: AWS Lambda Function URLs: Built-in HTTPS Endpoints for Single-Function Microservices

... sometimes all you need is a simple way to configure an HTTPS endpoint in front of your function without having to learn, configure, and operate additional services besides Lambda.

The Lambda basically spins off it's own express server and communicates directly with the internet, without imposing API Gateway limits on the communication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM