0

I use gunicorn to work with flask application. Here's my project structure.

flask-application
|-- non_web/
|   |--...
|
|-- web/
    |--app/
    |  |--api/
    |  |  |--...
    |  |--...
    |  |--__init__.py  # Here's def create_app()
    |
    |--config.py  # Here's config of flask application
    |--config_gunicorn.py  # Here's config of gunicorn
    |--run.py

When I execute command line [XXX@XXXX flask-application]# gunicorn -c web/config_gunicorn.py web.run:app, here raised a error ImportError: cannot import name 'XXX' from 'web.run' (/root/flask-application/web/run.py)

Here's run.py

```python
from web.app import create_app
app, whitelist = create_app(conf_type='default')

if __name__ == '__main__':
    app.run(host=app.config['HOST'], port=app.config['PORT'])
```

Here's __init__.py

```python
from flask import Flask
import redis

from ..config import config

def create_app(conf_type):
    app = Flask(__name__)
    app.config.from_object(obj=config[conf_type])
    config[conf_type].init_app(app=app)

    from .api import api as api_blueprint
    app.register_blueprint(blueprint=api_blueprint,
                           url_prefix='/api')
   
    whitelist = redis.StrictRedis(host=config[conf_type].REDIS_HOST,
                                  port=config[conf_type].REDIS_PORT,
                                  db=config[conf_type].REDIS_DB_WHITELIST,
                                  decode_responses=True)
    
    return app, whitelist
```

And config_gunicorn.py follows this: github.com/benoitc/gunicorn/blob/master/examples/example_config.py

It goes well when launching run.py without gunicorn on windows. Is there any help ? Thanks.

g4ls0n
  • 89
  • 2
  • 13
  • Can you share your `config_gunicorn.py` and `run.py`? Would also be good to know where this `XXX` module is located in your project folder hierarchy. – Ramon Moraes Aug 07 '20 at 13:54

1 Answers1

0

Here comes my answer. It turn out to be related with returned whitelist object of def create_app(). By creating a connection pool, I solved this problem when working with gunicorn. This post Correct Way of using Redis Connection Pool in Python helped me a lot.

g4ls0n
  • 89
  • 2
  • 13