首页 > 解决方案 > 运行python脚本时出现gunicorn错误

问题描述

这是我的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 仍然出现上述错误。请看看我在哪里犯了错误。

标签: pythongunicornaiohttp

解决方案


由于您没有直接运行文件“main.py”,因此变量的值__name__不是__main__but main。你需要改变你的if陈述:

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

推荐阅读