안홍경
안홍경

Reputation: 43

Flask "(" unexpected error when using gunicorn

I tried to deploy my flask app using gcloud, gunicorn. But when I wrote the app.yaml like below, it didn't work.

Here is my app.yaml file.

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT flask_todo:create_app

runtime_config:
  python_version: 3

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

init.py file

import os
from flask import Flask,render_template,request,url_for,redirect
from . import db


def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        app.config.from_pyfile('config.py', silent=True)

    else:
        app.config.from_mapping(test_config)


    try:
        os.makedirs(app.instance_path)

    except OSError:
        pass
    return app

flask_todo #this is where I cd to and run gunicorn flask_todo:create_app

flask_todo
--db.py
--__init__.py
--app.yaml
--requirements.txt

What is the reason? Please help me..

Upvotes: 0

Views: 372

Answers (2)

Juan P.
Juan P.

Reputation: 1

As the Flask documentation suggests, you should run you gunicorn command as:

# equivalent to 'from hello import create_app; create_app()'
$ gunicorn -w 4 'hello:create_app()'

then wrap your create_app command with quotes so your app.yaml is like this:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT 'flask_todo:create_app'

runtime_config:
  python_version: 3

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

Upvotes: 0

Dustin Ingram
Dustin Ingram

Reputation: 21520

Your arguments to gunicorn needs to be a variable in a module that is importable, and represents a WSGI app. In your example, create_app is a function that returns such a variable, not a variable itself.

Something like the following should work:

app.yaml:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT flask_todo:app

runtime_config:
  python_version: 3

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

__init__.py:

import os
from flask import Flask,render_template,request,url_for,redirect
from . import db


def create_app(test_config=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )

    if test_config is None:
        app.config.from_pyfile('config.py', silent=True)

    else:
        app.config.from_mapping(test_config)


    try:
        os.makedirs(app.instance_path)

    except OSError:
        pass
    return app

app = create_app()

Upvotes: 1

Related Questions