1

I have a flask application that I have configured as a python module.

To explain further, this is a simplified structure of my project:

project_dir
  ven
  dir_a
      dir_b
           flask_app
                 __init__.py
                 __main__.py
# __main__.py
.
.
.
app = create_app(...)
client = Client(app)
.
.
.
print("Hello World")
running_app = app.run("0.0.0.0", port=5000)

When I want to run my application, I hit python -m dir_a.dir_b.flask_app

Now I want to run it with gunicorn.

For your reference, this is a sample of running gunicorn

gunicorn -w 1 -b 0.0.0.0:5000 **wsgi:server**

If I want to run the application, from project_dir I run

python -m dir_a.dir_b.flask_app

How should I run my application with gunicorn in my case?

Please note that I want "Hello World" to be printed before running the application


What I have tried:

gunicorn -w 1 -b 0.0.0.0:5000 dir_a.dir_b.flask_app:running_app

I then removed the line running_app = app.run("0.0.0.0", port=5000) and tried

gunicorn -w 1 -b 0.0.0.0:5000 dir_a.dir_b.flask_app:app

and

gunicorn -w 1 -b 0.0.0.0:5000 dir_a.dir_b.flask_app:create_app(...)

None of them worked

Amin Ba
  • 1,603
  • 1
  • 13
  • 38

1 Answers1

1

You need to tell gunicorn what app to run

The app.run() method is for the development server, so first you’ll have to comment it out or just delete it.

And then

gunicorn -w 1 -b 0.0.0.0:5000 dir_a.dir_b.flask_app:app

Or you could import the app further up in the module.

ljmc
  • 4,830
  • 2
  • 7
  • 26
  • I edited the question – Amin Ba Dec 16 '21 at 22:31
  • btw, this is not answering my question – Amin Ba Dec 16 '21 at 22:32
  • No need to edit the question, I meant in the code. Basically, app.run() will start Flask’s development server, it’s decent for development, but like you probably read, gunicorn or uWSGI are better for prod. So step one to running an app with gunicorn is not starting the development server. Then you just point gunicorn to the right app (that same app whose app.run() you removed). – ljmc Dec 16 '21 at 22:34
  • please look at this tutorial, it is doing what I do https://towardsdatascience.com/how-to-deploy-ml-models-using-flask-gunicorn-nginx-docker-9b32055b3d0 – Amin Ba Dec 16 '21 at 22:37
  • Ok, see my edits they show the exact specifications you’ve listed. By the way, this questions has been asked many times before. – ljmc Dec 16 '21 at 22:38
  • the point is that I have other pieces of code that are in my __main__.py file and I want them to be run. Is your solution going to run them as well? – Amin Ba Dec 16 '21 at 22:42
  • Is Hello World going to be printed? – Amin Ba Dec 16 '21 at 22:42
  • I have never used a `__main__.py` file before. All the code you need run in a module should be in `__init__.py` or other files imported by `__init__.py`. As long as you put things in `__init__.py`, they’ll be run on import. – ljmc Dec 16 '21 at 22:46
  • https://stackoverflow.com/questions/4042905/what-is-main-py – Amin Ba Dec 16 '21 at 22:49
  • Thank you for the link, my point still remains true, file is run on import, you shouldn’t start development server, and start with `gunicorn -t 1 [other options] module.flask:app`. https://docs.gunicorn.org/en/stable/run.html – ljmc Dec 16 '21 at 22:59
  • I should not do `app.run("0.0.0.0", port=5000)` in production ? Why not? What I do is that run it inside a docker container and bind ports, for example with `-p 3000:5000`, and my application is accessible on port 3000 – Amin Ba Dec 16 '21 at 23:24
  • I tested what you advised and it did not work. I have reflected it in the edit of the question – Amin Ba Dec 16 '21 at 23:58
  • 1
    You should not do `app.run(…)` in production because it’s the [development server](https://flask.palletsprojects.com/en/2.0.x/server/#development-server). You need to use a better options for prod, they are listed [here](https://flask.palletsprojects.com/en/2.0.x/deploying/) and gunicorn is [here](https://flask.palletsprojects.com/en/2.0.x/deploying/wsgi-standalone/#gunicorn), my best guess is `__main__.py` is not executed by gunicorn only by python when you run module as program, which is why my `__init__.py` comment is valid, move your code to imported files and retry, that’s all I have. – ljmc Dec 17 '21 at 09:07