0

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?

Kajal
  • 709
  • 8
  • 27

1 Answers1

2

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.

Klaus D.
  • 13,874
  • 5
  • 41
  • 48
  • Yes! I typing out the same thing. More information can be found here: https://stackoverflow.com/questions/38876721/handle-flask-requests-concurrently-with-threaded-true – MacMcIrish Aug 17 '17 at 05:18
  • Thank you Klaus and MacMaclirish. I think making my application to run in multiple thread will solve my problem. And it's just a simple application which will be used by few people. – Kajal Aug 17 '17 at 05:23
  • Well, it would be a solution to run the server multi-threaded, but that would just mask the underlying problem. If you web app does not run in one thread there is something wrong with it. Also multiple thread will complicate debugging. – Klaus D. Aug 17 '17 at 05:23
  • 1
    @MacMcIrish, The link was so useful. I will defiantly consider all the topic discussed in that thread. – Kajal Aug 17 '17 at 05:24
  • If you use it for anything else then development use a WSGI setup. Guncorn can serve it simply. – Klaus D. Aug 17 '17 at 05:25
  • Sure Klaus, I will use WSGI in later phase. For now I added `app.run(host= '0.0.0.0',threaded=True)` and it solved my problem. – Kajal Aug 17 '17 at 05:40