Skip to content

Instantly share code, notes, and snippets.

@dcosson dcosson/
Last active Aug 29, 2015

What would you like to do?
Flask Hello
# Simple http server to test concurrency or load balancing or whatever,
# specifying numprocs greater than 1 will run that many instances of the
# flask app in subprocesses on incrementing ports beginning with the one specified
# Run:
# sudo pip install flask
# python [PORT-NUM] [NUMPROCS]
from flask import Flask
import os
import signal
import subprocess
import sys
app = Flask(__name__)
def hello_world():
return "Hello from {0}".format(os.getpid())
def run_multi_in_subprocs(numprocs, starting_port):
procs = []
for port in xrange(starting_port, starting_port + numprocs):
procs.append(subprocess.Popen(['python', __file__, str(port)]))
def cleanup_for_exit(signal, frame):
print "Caught SIGINT, killing {0} subproccesses and exiting".format(len(procs))
[p.kill() for p in procs]
signal.signal(signal.SIGINT, cleanup_for_exit)
if __name__ == '__main__':
port = int(sys.argv[1]) if len(sys.argv) > 1 else 5000
numprocs = int(sys.argv[2]) if len(sys.argv) > 2 else 1
if numprocs == 1:'', port=int(port))
run_multi_in_subprocs(numprocs, port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.