【问题标题】:gunicorn error while running python script运行python脚本时出现gunicorn错误
【发布时间】:2020-10-17 15:39:56
【问题描述】:

这是我的python代码。

    from aiohttp import web

    if __name__ == "__main__":
        app = web.Application()
        cors = aiohttp_cors.setup(app)

        app.on_shutdown.append(on_shutdown)
        app.router.add_get("/", index)
        app.router.add_get("/client.js", javascript)

        cors = aiohttp_cors.setup(app, defaults={
            "*": aiohttp_cors.ResourceOptions(
                    allow_credentials=True,
                    expose_headers="*",
                    allow_headers="*",
                )
        })
        resource = cors.add(app.router.add_resource("/offer"))
        cors.add(resource.add_route("POST", offer))

        resource = cors.add(app.router.add_resource("/image_makeup"))
        cors.add(resource.add_route("POST", image_makeup))
        
        web.run_app(
            app, access_log=None, host=args.host, port=args.port, ssl_context=ssl_context
        )

命令:

gunicorn main:app --bind localhost:8080 --worker-class aiohttp.GunicornWebWorker

错误:

    [2020-10-17 21:17:13 +0530] [47676] [INFO] Starting gunicorn 20.0.4
    [2020-10-17 21:17:13 +0530] [47676] [INFO] Listening at: http://127.0.0.1:8080 (47676)
    [2020-10-17 21:17:13 +0530] [47676] [INFO] Using worker: sync
    [2020-10-17 21:17:13 +0530] [47678] [INFO] Booting worker with pid: 47678
    usage: gunicorn [-h] [--cert-file CERT_FILE] [--key-file KEY_FILE] [--host HOST] [--port PORT] [--verbose]
    gunicorn: error: unrecognized arguments: --bind localhost:8080 wsgi:main
    [2020-10-17 21:17:18 +0530] [47678] [INFO] Worker exiting (pid: 47678)
    [2020-10-17 21:17:19 +0530] [47680] [INFO] Booting worker with pid: 47680

我的根目录中有 main.py 仍然出现上述错误。 请看看我在哪里犯了错误。

【问题讨论】:

  • 我更新了错误请检查

标签: python gunicorn aiohttp


【解决方案1】:

由于您没有直接运行文件“main.py”,变量__name__ 的值不是__main__,而是main。您需要更改您的 if 声明:

if __name__ == "main":
    app = web.Application()
    ...

【讨论】:

  • 嗨,皮埃尔,我用不同的错误更新了我的问题。请看一下。得到另一个错误
  • 你的命令行gunicorn main:app --bind localhost:8080 --worker-class aiohttp.GunicornWebWorker 是否从上一个错误中更新了?
【解决方案2】:

如给定in the documentation

运行 Gunicorn 时,您需要提供模块名称(在您的情况下为 main)、应用程序或应用程序工厂的名称,以及作为命令行标志或在您的配置文件中提供的其他 Gunicorn 设置。

入口点可以是应用程序实例,也可以是不接受参数并返回应用程序实例的协程

你的代码必须是这样的:

from aiohttp import web

async def my_web_app():
    app = web.Application()
    ....
    return app

from aiohttp import web

my_web_app = web.Application()
....

Gunicorn 命令变为:

gunicorn main:my_web_app --bind localhost:8080 --worker-class aiohttp.GunicornWebWorker

【讨论】:

    猜你喜欢
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2020-08-02
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多