python怎么启动flask_如何在单独的线程中的python脚本中启动flask应用程序?

I have a script that is extracting data from an internal service and storing the count of data types in memory like this:

metrics = {

"new_accounts": 152,

"acquisitions": 2005,

...

}

That is being done in memory and that whole process is kicked off by the subscribe method in this script:

if __name__ == "__main__":

loop = asyncio.get_event_loop()

for signal in [signal.SIGHUP, signal.SIGTERM, signal.SIGINT]:

loop.add_signal_handler(

signal, lambda s=signal: asyncio.create_task(close_subscriptions(s, loop)))

for subscription in SUBSCRIPTION_TYPES:

loop.create_task(subscribe(subscription))

loop.run_forever()

I want to expose that metrics data through an endpoint in flask for monitoring purposes. In a thread, can I start the flask app in this script like this:

if __name__ == "__main__":

...

for subscription in SUBSCRIPTION_TYPES:

loop.create_task(subscribe(subscription))

...

loop.run_forever()

Does this make sense? I already have the flask app running locally but it doesn't have access to this metrics data. Is this the way I need to go about this?

解决方案

Try to put Your metric to Queue:

Flask endpoint can read all data from Queue, and expose last value as endpoint data

Cheers, Fenrir

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用pyinstaller打包uwsgi启动Flask应用程序需要在命令行窗口输入以下命令:pyinstaller --add-data "<uwsgi_config_file>:." --add-data "static:static" --add-data "templates:templates" -F <uwsgi_entry_point.py>。其,<uwsgi_config_file>表示uwsgi配置文件的路径,<uwsgi_entry_point.py>表示uwsgi启动的入口文件的路径。 ### 回答2: 使用pyinstaller打包uwsgi启动Flask应用程序有以下几个步骤: 1. 安装pyinstaller和uwsgi库。可以使用pip安装这两个库: ``` pip install pyinstaller uwsgi ``` 2. 编写uwsgi启动脚本。创建一个名为`uwsgi.ini`的文件,然后在文件添加以下内容: ``` [uwsgi] module = your_app:app master = true processes = 1 socket = your_app.sock chmod-socket = 660 vacuum = true die-on-term = true ``` 这个脚本配置uwsgi的一些参数,例如指定`module`为你的Flask应用的入口文件和应用实例的名字,设置`socket`用于和Nginx或其他服务器通信等参数。 3. 编写pyinstaller打包脚本。创建一个名为`build.py`的文件,然后在文件添加以下内容: ```python import PyInstaller.__main__ PyInstaller.__main__.run([ '--name=your_app', '--onefile', 'your_app.py', # Flask应用的入口文件 ]) ``` 4. 执行脚本进行打包。在命令行运行以下命令: ``` python build.py ``` 这将使用pyinstaller将Flask应用程序打包成一个可执行文件。 5. 配置Nginx。将Nginx的配置文件添加以下内容: ``` location / { include uwsgi_params; uwsgi_pass unix:///path/to/your_app.sock; # 替换为uwsgi.ini配置的socket路径 } ``` 这将指示Nginx将请求转发到uwsgi处理。 6. 启动应用程序。在命令行运行以下命令启动Flask应用程序: ``` ./your_app ``` 通过以上步骤,你就可以使用pyinstaller打包uwsgi启动Flask应用程序,并且可以使用Nginx作为反向代理转发请求。注意替换相关路径和文件名为你自己的。 ### 回答3: 使用pyinstaller打包uwsgi启动Flask应用程序需要经过以下几个步骤: 1. 首先,确保已经安装了pyinstaller、uwsgi以及Flask等相关依赖库。 2. 创建一个uwsgi的配置文件,例如uwsgi.ini,配置文件内可以包含启动uwsgi的一些参数,比如监听的端口、线程数量等。配置文件内容可以参考uwsgi官方文档进行设置。 3. 在Flask应用程序的主文件(通常为app.py或者主函数所在的文件),使用uwsgi提供的启动接口来启动Flask应用程序。例如,在app.py使用以下代码: ```python from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, world!' if __name__ == '__main__': app.run() ``` 4. 在命令行使用pyinstaller命令来进行打包,例如: ```bash pyinstaller --onefile app.py ``` 上述命令会在当前目录下生成一个dist文件夹,内含打包好的可执行文件。 5. 将生成的可执行文件、uwsgi.ini文件以及其他必要文件(如静态文件、模板文件等)放置在同一个文件夹。 6. 打开命令行,进入到上述文件夹,执行以下命令来启动应用程序: ```bash uwsgi uwsgi.ini ``` 这样就可以使用pyinstaller打包uwsgi启动Flask应用程序了。注意,使用pyinstaller打包的可执行文件是一个独立的应用程序,不依赖于系统是否已经安装了Python及相关库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值