1
@app.route("/")
def start():
    #will do some task
    return 'completed'

In the above program, after execution the 1st request 2nd request will execute. But I want to make such a server that will accept, execute and response multiple requests at a certain time parallelly by using flask or anything else.

How will I make this?

Saikat Kundu
  • 350
  • 1
  • 15

2 Answers2

2

For multi-request handling/production deployment, gunicorn or apache or gevent has to be used.

http://flask.pocoo.org/docs/0.11/deploying/

Similar approach follows for other python web frameworks too like Django.

SaiNageswar S
  • 1,203
  • 13
  • 22
  • can u give me any example that can help me to check paralley handling request by using django ?? thanks – Sudip Das Dec 21 '16 at 11:49
  • 1
    @SudipDas Django can be deployed using Apache mod_wsgi as described in https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi I have used gunicorn as well, however, gunicorn doesn't serve static files and you would require to further interface it with nginx to serve static files. For further parallelisation one can use docker swarm and scale service or use Elastic Beanstalk and scale dynamically.. – SaiNageswar S Dec 21 '16 at 16:20
1

You can use klein module, which handle multiple requests in a time.

  • Please refer the following lin which will give clear explanation about limiting in FLASK.

Comparison between Flask and Klein

After refering this link I switched from Flask to Klein. Hope it helps you too.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
Karthikeyan KR
  • 1,134
  • 1
  • 17
  • 38
  • I also need this one for distributed cluster computing,suppose request load of the server m/c is to high then i want to shift the request another computer those are connected to the server. Generally i use dask.distributed to manage distributed cluster computing. can i shift request by using klein??mainly i wan to shift the request is load of the server is to high. – Sudip Das Dec 21 '16 at 11:14
  • Please refer their documentation : https://media.readthedocs.org/pdf/klein/latest/klein.pdf – Karthikeyan KR Dec 21 '16 at 11:15
  • sorry for disturbing, can u send me any example that can handle multiple request a same time? by using this example ==>from klein import Klein import time app = Klein() def testFun(): print('Starting') while True: time.sleep(3) print('looping') time.sleep(3) print('3 Seconds Later') @app.route('/', methods = ['GET']) def square_submit(request): # x = int(request.args.get('x', [0])[0]) # res = x * x testFun() return "completed" if __name__ == "__main__": app.run("localhost", 8888) – Sudip Das Dec 21 '16 at 11:44
  • Handling multiple request is different from processing multiple request. Klein will handle large volume of request at a time. But process one by one. But in Flask even if you send more than three request when one was executing it will crashed. You can also activate thread in Klein for multiple processing. – Karthikeyan KR Dec 21 '16 at 12:31
  • if i run a infinite loop after coming the 1st request then 2nd request will be stored in queue. BT i don't want to store that 2nd request in queue. I want to accept the 2nd request and both request will execute the infinite loop function intermediately. klein can do this?? – Sudip Das Dec 21 '16 at 12:39
  • 1
    from klein import Klein from twisted.internet import defer from twisted.internet import threads import time app = Klein() def testFun(request): print(request) while True: time.sleep(3) print('looping') time.sleep(3) print('3 Seconds Later') @app.route('/', methods = ['GET']) def square_submit(request): #testFun(request) threads.deferToThread(testFun, request) return "hello" # res = x * x testFun() return "completed" if __name__ == '__main__': app.run("localhost", 8888) – Karthikeyan KR Dec 21 '16 at 13:33
  • okk thanks it is working, here testFun() is not returning any thing, bt suppose if testFun() return some value after some time, and i want that value to return to the bouser. then how i will do this?? – Sudip Das Dec 21 '16 at 13:43
  • please refer the following link, which will contains the solution, http://twistedmatrix.com/documents/13.2.0/core/howto/threading.html – Karthikeyan KR Dec 21 '16 at 14:17
  • did you get it? – Karthikeyan KR Dec 29 '16 at 07:50