简体   繁体   中英

How can I run this flask app using gunicorn

I Refactored my code after making it which turns out to be a mistake and now I am not able to get it run using gunicorn. If you have advice for structuring or factoring from here on then that's welcome too. I am trying to get it run from 5 days now, Struggling a lot.

Directory Structure

app--
     --db.sqlite
     --__init__.py
     --models.py
     --MyFunctions.py
     --routes.py
     --schemas.py
     --sockets.py
app.py

Inside app.py

from app import *


from app.MyFunctions import *
from app.models import *
from app.schemas import *
from app.routes import *
from app.sockets import *


port=os.environ.get("PORT")

if port is None or port == "":
    port = 3000

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=port) # when running on heroku

Inside init .py

import jwt
from flask import (
    Flask,
    request,
    jsonify,
    make_response,
)
from flask_socketio import (
    SocketIO, 
    send, 
    emit, 
    join_room, 
    leave_room,
)
from sqlalchemy import (
    Column,
    Integer,
    String,
    Boolean,
    ForeignKey,
    DateTime,
    Sequence,
    Float,
)
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from os import environ


def debug(msg):
    print('\n\n\n'+str(msg)+'\n\n\n'+'\n\nType:\t'+str(type(msg))+'\n\n')

from .models import *
socketio = SocketIO(app, cors_allowed_origins='*')

Inside models.py (only import related things not models, my most of the imports are lying here only)

from flask import (
    Flask,
    request,
    jsonify,
    make_response,
    redirect,
)
from flask_socketio import (
    SocketIO, 
    send, 
    emit, 
    join_room, 
    leave_room,
)
from sqlalchemy import (
    Column,
    Integer,
    String,
    Boolean,
    ForeignKey,
    DateTime,
    Sequence,
    Float,
)
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_bcrypt import Bcrypt
from os import environ
from flask_cors import CORS, cross_origin



app = Flask(__name__)
root = os.path.dirname(os.path.abspath(__file__))
CORS(app, support_credentials=True)
basedir = os.path.abspath(os.path.dirname(__file__))

PG = os.environ.get("DATABASE_URL")

if PG is None or PG=="":
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + \
        os.path.join(basedir, 'db.sqlite')
else:
    app.config['SQLALCHEMY_DATABASE_URI'] = PG


app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = environ.get('STRIPE_API_KEY')


db = SQLAlchemy(app)

ma = Marshmallow(app)

bcrypt = Bcrypt(app)

socketio = SocketIO(app, cors_allowed_origins='*')

edit: Currently I am using python only to run the app on heroku so my Procfile looks like

web: python app.py

And yes I have tried many other formats like

web: gunicorn app:app
web: gunicorn models:app
web: gunicorn app/models:app

Tried by making different run file also and many other methods I am afraid that I need to change it's structure to make it work.

I renamed my app.py file and then it worked

Inside Procfile

web: gunicorn gettingstarted.wsgi
web: gunicorn runner:app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM