Kajal Kajal - 1 year ago 66
Python Question

Flask : Server not responding on internal API access

I was trying develop web application using flask, below is my code,

from sample import APIAccessor
#API
@app.route('/test/triggerSecCall',methods=['GET'])
def triggerMain():
resp = APIAccessor().trigger2()
return Response(json.dumps(resp), mimetype='application/json')

@app.route('/test/seccall',methods=['GET'])
def triggerSub():
resp = {'data':'called second method'}
return Response(json.dumps(resp), mimetype='application/json')


And my trigger method contains the following code,

def trigger2(self):
url = 'http:/127.0.0.1:5000/test/seccall'
response = requests.get(url)
response.raise_for_status()
responseJson = response.json()
if self.track:
print 'Response:::%s' %str(responseJson)
return responseJson


When I hit
http://127.0.0.1:5000/test/seccall
, I get the expected output. When I hit
/test/triggerSecCall
, the server stop responding. The request waits forever.
At this stage, I am not able to access any apis from anyother REST clients. When I force stop the server(Ctrl+C) I am getting response in the
second
REST client.

Why flask is not able to serve to internal service call?

Answer Source

I guess you are using the single threaded development server and not a WSGI setup for production.

Since the server has only one thread is can handle one request at a time. The first request will be executed, resulting in the requests.get(...) which will open a second request that can not be handled until the first request is complete, a dead lock.

The best solution would be to just call triggerSub() to get the result instead of using an HTTP request.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download