Python中怎么使用Flask实现进度条
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,本篇内容主要讲解"Python中怎么使用Flask实现进度条",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Python中怎么使用Flask实现进度条"吧!
千家信息网最后更新 2025年02月23日Python中怎么使用Flask实现进度条
本篇内容主要讲解"Python中怎么使用Flask实现进度条",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"Python中怎么使用Flask实现进度条"吧!
使用Flask实现进度条
问题描述
Python异步处理,新起一个进程返回处理进度
解决方案
使用 tqdm 和 multiprocessing.Pool
安装
pip install tqdm
代码
import timeimport threadingfrom multiprocessing import Poolfrom tqdm import tqdmdef do_work(x): time.sleep(x) return xdef progress(): time.sleep(3) # 3秒后查进度 print(f'任务有: {pbar.total} 已完成:{pbar.n}')tasks = range(10)pbar = tqdm(total=len(tasks))if __name__ == '__main__': thread = threading.Thread(target=progress) thread.start() results = [] with Pool(processes=5) as pool: for result in pool.imap_unordered(do_work, tasks): results.append(result) pbar.update(1) print(results)
效果
Flask
安装
pip install flask
main.py
import timefrom multiprocessing import Poolfrom tqdm import tqdmfrom flask import Flask, make_response, jsonifyapp = Flask(__name__)def do_work(x): time.sleep(x) return xtotal = 5 # 总任务数tasks = range(total)pbar = tqdm(total=len(tasks))@app.route('/run/')def run(): """执行任务""" results = [] with Pool(processes=2) as pool: for _result in pool.imap_unordered(do_work, tasks): results.append(_result) if pbar.n >= total: pbar.n = 0 # 重置 pbar.update(1) response = make_response(jsonify(dict(results=results))) response.headers.add('Access-Control-Allow-Origin', '*') response.headers.add('Access-Control-Allow-Headers', '*') response.headers.add('Access-Control-Allow-Methods', '*') return response@app.route('/progress/')def progress(): """查看进度""" response = make_response(jsonify(dict(n=pbar.n, total=pbar.total))) response.headers.add('Access-Control-Allow-Origin', '*') response.headers.add('Access-Control-Allow-Headers', '*') response.headers.add('Access-Control-Allow-Methods', '*') return response
启动(以 Windows 为例)
set FLASK_APP=mainflask run
接口列表
执行任务:http://127.0.0.1:5000/run/
查看进度:http://127.0.0.1:5000/progress/
test.html
进度条
效果
Flask使用简单异步任务
在Flask中使用简单异步任务最简洁优雅的原生实现:
from flask import Flaskfrom time import sleepfrom concurrent.futures import ThreadPoolExecutor# DOCS https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutorexecutor = ThreadPoolExecutor(2)app = Flask(__name__)@app.route('/jobs')def run_jobs(): executor.submit(some_long_task1) executor.submit(some_long_task2, 'hello', 123) return 'Two jobs was launched in background!'def some_long_task1(): print("Task #1 started!") sleep(10) print("Task #1 is done!")def some_long_task2(arg1, arg2): print("Task #2 started with args: %s %s!" % (arg1, arg2)) sleep(5) print("Task #2 is done!")if __name__ == '__main__': app.run()
到此,相信大家对"Python中怎么使用Flask实现进度条"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
进度
任务
内容
效果
处理
学习
实用
更深
简洁
优雅
代码
兴趣
实用性
实际
总任务
接口
操作简单
方案
方法
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
公交网络安全法
日常生活中的关系数据库系统
ios系统软件开发收费明细
codol与服务器
平面设计手机软件开发学习
软件开发主要技术经济指标
河北工控软件开发定做
检察院开展网络安全阀培训
计算机网络技术中国大牛
怎样新建一个数据库用户
小程序云开发数据库图片路径
网络舆情与网络安全学习心得
软件开发部件
异构多核服务器处理器
全球大企业接连被袭击网络安全
iis服务器管理器
亚马逊云服务器收费退回
芜湖飞慧网络技术有限公司
以色列为什么是网络安全强国
测试如何管理服务器
数据库备份文件看不到
广西网络安全等级保护大会
数据库中插入函数平均数
怎么看戴尔笔记本服务器
服务器sata接口安装ssd
南京智能化软件开发排名靠前
软件开发平均风险系数是多少
本机连接虚拟机数据库
科技项目软件开发费列支
铁岭新零售软件开发设计