Kajal Kajal -3 years ago 132
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
def triggerMain():
resp = APIAccessor().trigger2()
return Response(json.dumps(resp), mimetype='application/json')

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:/'
response = requests.get(url)
responseJson = response.json()
if self.track:
print 'Response:::%s' %str(responseJson)
return responseJson

When I hit
, I get the expected output. When I hit
, 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
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