2

I have a flask app. I want the client-server connection to terminate if the server does not respond within a stipulated time (say 20 seconds). I read here that the session.permanent = True can be set. I am a bit unclear where this goes in the server side code (if at all this is the way??).

For simplicity I am including the minimal server side code I have. Actually the server is performing a File Read/Write operation and returning a result to the client.

from flask import Flask, session, app
from flask_restful import Api, Resource
from datetime import timedelta

app = Flask(__name__)
api = Api(app)

class GetParams(Resource):
    def get(self):
        print ("Hello.")
        return 'OK'

api.add_resource(GetParams, '/data') 
if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5002) 

Can anyone tell me what should I do here so that the connection between my client and server is terminated if the server does not respond i.e., send data back to the client within 20 seconds?

  • `session.permanent` is not going to help you. Are you sure that it is relevant to set a connexion time ? – Cryckx Mar 12 '19 at 17:08
  • I am not really sure if that is or not. I read in one of the answers that timeout can be implemented in Flask sessions with that. It's possible that I am wrong. Can you tell me what can help in my case? –  Mar 12 '19 at 17:14
  • session.permanent is for client sessions, not for requests. If you want to set timeout for request, not sure if the wsgi server flask comes with supports timeout (i couldn't find any reference). Also, flask in-built wsgi server isn't recomended for production use. You might as well start looking at alternate options (see http://flask.pocoo.org/docs/1.0/deploying/) and these servers might be more robust – ranjith Mar 12 '19 at 17:14
  • couldn't find any reference as well @ranjith – Cryckx Mar 12 '19 at 17:15
  • @ranjith, so does that mean that if I want to implement a session timeout in my development environment it won't be possible? –  Mar 12 '19 at 17:16
  • do you want to implement session timeout (this can be done) or timeout for the request if the underlying app isn't responding in stipulated time? – ranjith Mar 12 '19 at 18:10
  • @ranjith, I think my question wasn't framed properly. What I want is a timeout for the request. In my Client code I am doing a `session = requests.Session()` followed by `response = session.get(url, params=json.dumps(request)`. I want to get a timeout message in this `response` variable in case the server does not respond within 20 seconds. –  Mar 12 '19 at 18:54
  • I have edited by question so that there is no confusion. –  Mar 12 '19 at 18:56
  • your question is on the requests library ? Would be better if you could provide client code that you are talking about and explain expected outcome using that – ranjith Mar 12 '19 at 19:40

2 Answers2

1

Long running tasks should be dealt with in a different design because, if you allow your server to keep a request alive for 50 minutes, you can't force user browser to do so.

I would recommend implementing the long running task as a thread that notifies the user once it's done.

For more readings about the problem statement and suggested solutions:

  1. timeout issue with chrome and flask
  2. long request time patterns
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
0

I believe that the only thing you need is to put your connexion statement in a try/except block. So that you will be able to handle any kind of connexion error.


Furthermore, a session timeout and a connexion fail/unreachable server are different things. A session timeout disconnect a user from a server which is here for too long (usually used to avoid a user to forgot a session open). Whereas when a server is unreachable the user isn't connected so there is no session timeout.


from flask import Flask, session, app
from flask_restful import Api, Resource
from datetime import timedelta

app = Flask(__name__)
api = Api(app)

class GetParams(Resource):
    def get(self):
        print ("Hello.")
        return 'OK'

api.add_resource(GetParams, '/data') 
if __name__ == '__main__':
      try:
         app.run(host='130.0.1.1', port=5002)
      except:
         print("unexcepted error")

you could qualify the received exception, but you'll have to read a bit of doc http://flask.pocoo.org/docs/1.0/quickstart/#what-to-do-if-the-server-does-not-start

Cryckx
  • 659
  • 6
  • 18
  • This prints some error message in case an exception occurs but my question is whether there is a way to terminate the client-server connection if the server does not send a reply within a stipulated time period? –  Mar 12 '19 at 17:53