0

I have a python Flask listener waiting on port 8080.I expect a client to POST documents to this listener.

#!/usr/bin/env python2
from __future__ import print_function
from flask import Flask, request
from flask.ext.cors import CORS
from datetime import datetime
import os, traceback, sys 
import json
import os

app = Flask('__name__')
cors = CORS(app)

@app.route('/',methods=['GET','POST','OPTIONS'])                                                                                                                                         
def recive_fe_events():
    try:
        data = request.get_data()

        if request.content_length < 20000 and request.content_length != 0:
            filename = 'out/{0}.json'.format(str(datetime.now()))
            with open(filename, 'w') as f:
                 f.write(data)

            print('Wrote', filename)
        else:
            print("Request too long", request.content_length)
            content = '{{"status": 413, "content_length": {0}, "content": "{1}"}}'.format(request.content_length, data)
            return content, 413 
    except:
        traceback.print_exc()
        return None, status.HTTP_500_INTERNAL_SERVER_ERROR

    return '{"status": 200}\n'

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=False,port=8080)

This server is able to listen to POST messages and decode them and dump them to a file.However there is a concern that the frequency of POST messages may become very high. I am thinking about ways to make this application scalable. What is the best way.Do I implement a load balancer in front, listening on port 8080 and then balance traffic across multiple listeners on 8081,8082 and 8083 for example.

liv2hak
  • 14,472
  • 53
  • 157
  • 270
  • Look at depolying with Gunicorn, it supports multiple processes. Don't use Flask's builtin server, that's just for development. – nathancahill May 20 '15 at 23:42
  • http://flask.pocoo.org/docs/0.10/deploying/wsgi-standalone/ – nathancahill May 20 '15 at 23:44
  • This is way too broad of a question (and probably too subjective, too); people have written whole blogs on the various ways to build scalable web apps around Flask. See, for example, [Disqus's blog post on how they did it](http://highscalability.com/blog/2014/4/28/how-disqus-went-realtime-with-165k-messages-per-second-and-l.html). That's not the kind of thing you'd want to write in a SO answer, and it's not going to apply to everyone, nor is it the objectively "right" answer, just one of many possible answers that worked. – abarnert May 20 '15 at 23:50
  • So, I think you need to find a more discussion-oriented forum to post this question on instead of StackOverflow. – abarnert May 20 '15 at 23:51
  • @nathancahill - what do you mean by Flask's builtin server? – liv2hak May 21 '15 at 00:15
  • When you do `app.run()` it starts a server in a single process. But it's not meant for production – nathancahill May 21 '15 at 00:17

0 Answers0