可以在setup.py中使用入口点以编程方式运行uvicorn吗?

问题描述 投票:0回答:1

有关以编程方式运行的 uvicorn 文档建议:

你应该将 uvicorn.run 放入主模块中的 if name == 'main' 子句中

示例:

if __name__ == "__main__":
    uvicorn.run("service:app", host="0.0.0.0", port=8000, reload=True)

有没有办法使用

setup.py
入口点启动服务器:

尝试过:

'entry_points': {
    'console_scripts': [
        'my_service=my_service.service:main',
    ],
},

然后设置:

def main():
    uvicorn.run("my_service.service:app", host="0.0.0.0", port=8000, reload=True)

但是,现在它不作为顶级脚本运行。

是否可以像 uvicorn 所建议的那样既满足入口点又满足运行?

python fastapi uvicorn
1个回答
0
投票

是的,这是可能的。 console_scripts 入口点引用了一个函数,因此您必须修改

uvicorn
文档中的示例:

def serve():
    uvicorn.run("my_service.service:app", host="0.0.0.0", port=8000, reload=True)

if __name__ == "__main__":
    serve()

那么工作入口点将是:

'entry_points': {
    'console_scripts': [
        'my_service=my_service.service:main',
    ],
},

假设

my_service
为包名称,
service
为模块名称。

© www.soinside.com 2019 - 2024. All rights reserved.