作为线程启动Flask服务器

2024-05-19 08:59:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想启动一个Flask服务器,它是由SwaggerUI作为另一个Python脚本中的线程创建的。启动它的命令使用-m标志(python -m swagger_server)。如何在Python脚本中使用-m标志

我试着使用runpy模块

runpy.run_module('swagger_server') 

但它只是执行并完成,什么也不做


Tags: 模块run命令服务器脚本flaskserver标志
1条回答
网友
1楼 · 发布于 2024-05-19 08:59:38

大多数包只在__main__范围内运行,以防止在导入时运行

runpy.run_module()基于run_name参数设置__name__

__name__ is set to run_name if this optional argument is not None, to mod_name + '.__main__' if the named module is a package and to the mod_name argument otherwise. https://docs.python.org/3/library/runpy.html

您可以通过将run_name设置为__main__来运行包:

runpy.run_module('swagger_server', run_name='__main__')

相关问题 更多 >

    热门问题