简体   繁体   English

如何使用Gunicorn在Falcon中启动应用程序

[英]How to start application in Falcon with Gunicorn

I have simple application, 我有简单的申请,

# app.py
import falcon

class ThingsResource:
def on_get(self, req, resq) :
    # something

class SomeResource:
    def on_get(self, req, resq) :
        # something

def create_things(self):    
    app = falcon.API(middleware=[(Middleware1())])
    things = ThingsResource()
    app.add_route('/things', things)

def create_some(self):
    app = falcon.API(middleware=[(Middleware2(exempt_routes=['/health']))])
    some = SomeResource()
    app.add_route('/some', some)

The problem is that, because i have different middleware for route's for one route is Middleware1 and for another is Middleware2 问题是,因为我对路由使用不同的中间件,所以一条路由是Middleware1,另一条是Middleware2

I need run app.py application, but this: 我需要运行app.py应用程序,但这是:

gunicorn -b 0.0.0.0:8000 app --reload

[Failed to find application object 'application' in 'app'] [无法在“应用程序”中找到应用程序对象“应用程序”]

not work 不行

I do not know how to run this application 我不知道如何运行此应用程序

I should run 我应该跑

gunicorn -b 0.0.0.0:8000 app:app --reload

But 'app' it's inside the method 但是“ app”位于方法内部

Someone has an idea? 有人有主意吗?

What you can do is, return app instance from these function and assign it to a variable in your file (outside any function) like this: 您可以做的是,从这些函数返回app实例,并将其分配给文件中的变量(在任何函数之外),如下所示:

def create_things():    
    app = falcon.API(middleware=[(Middleware1())])
    things = ThingsResource()
    app.add_route('/things', things)

    return app

def create_some():
    app = falcon.API(middleware=[(Middleware2(exempt_routes=['/health']))])
    some = SomeResource()
    app.add_route('/some', some)

    return app


app = create_some()

and run it using 并使用运行

gunicorn -b 0.0.0.0:8000 <file_name>:app --reload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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