1

So I built a python app in flask, and I want to do some load testing.

For that I want to run it over multiple ports. Currently when I do

flask run

it by default runs on port 5000, but I want it to also run on 5001 and 5002.

How do I do that?

aroma
  • 1,370
  • 1
  • 15
  • 30
  • why would you run it on multiple ports? i think you want to have your one port app be able to handle many connections, no? – Nullman Aug 01 '19 at 11:59
  • @Nullman Yes. that's correct. But running on various ports will let me run more threads than just running on single port. – aroma Aug 01 '19 at 12:02
  • that will let you stress your machine, not your app. one app (AFAIK) can only run on one port, what you want to do is let it have more threads, like [this](https://stackoverflow.com/a/14823968/7540911) – Nullman Aug 01 '19 at 12:04
  • This is an [XY problem](https://xyproblem.info/). What you actually want to do is use more threads to handle incoming requests. Binding to multiple ports is the wrong way to accomplish that. You should use a production-ready WSGI server such as [Gunicorn](https://gunicorn.org/) which allows you to specify the number of request handler threads as a configuration parameter. – Resigned June 2023 Jun 12 '22 at 21:22

2 Answers2

1

You might want to consider running the service under Gunicorn

http://docs.gunicorn.org/en/stable/run.html

Gunicorn has a --workers option which allows you to specify the number of instances of your service to run.

The nice thing about using Gunicorn is you don't need to worry about starting up/stopping multiple instances of your service by hand as it deals with it for you.

The other nice thing is you'll have a single interface to the multiple workers running under it.

This article explains how to add Gunicorn to a Flask project.

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-14-04

Mat
  • 1,345
  • 9
  • 15
0

You can specify the port in environment variable, as explained here:

export FLASK_RUN_PORT=5000
flask run & 
export FLASK_RUN_PORT=5001
flask run &
export FLASK_RUN_PORT=5002
flask run &
zmbq
  • 38,013
  • 14
  • 101
  • 171