脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - Python异步处理返回进度——使用Flask实现进度条

Python异步处理返回进度——使用Flask实现进度条

2022-12-26 13:27XerCis Python

这篇文章主要介绍了Python异步处理返回进度——使用Flask实现进度条,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

使用Flask实现进度条

问题描述

Python异步处理,新起一个进程返回处理进度

解决方案

使用 tqdm 和 multiprocessing.Pool

安装

?
1
pip install tqdm

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import time
import threading
from multiprocessing import Pool
from tqdm import tqdm
def do_work(x):
    time.sleep(x)
    return x
def 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)

效果

Python异步处理返回进度——使用Flask实现进度条

Flask

安装

?
1
pip install flask

main.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import time
from multiprocessing import Pool
from tqdm import tqdm
from flask import Flask, make_response, jsonify
app = Flask(__name__)
def do_work(x):
    time.sleep(x)
    return x
total = 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 为例)

?
1
2
set FLASK_APP=main
flask run

接口列表

  • 执行任务:http://127.0.0.1:5000/run/
  • 查看进度:http://127.0.0.1:5000/progress/

test.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>进度条</title>
    <script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow"  rel="stylesheet">
</head>
<body>
<button id="run">执行任务</button>
<br><br>
<div class="progress">
    <div class="progress-bar" role="progressbar" aria-valuenow="1" aria-valuemin="0" aria-valuemax="100"
         style="width: 10%">0.00%
    </div>
</div>
</body>
<script>
    function set_progress_rate(n, total) {
        //设置进度
        var rate = (n / total * 100).toFixed(2);
        if (n > 0) {
            $(".progress-bar").attr("aria-valuenow", n);
            $(".progress-bar").attr("aria-valuemax", total);
            $(".progress-bar").text(rate + "%");
            $(".progress-bar").css("width", rate + "%");
        }
    }
    $("#run").click(function () {
        //执行任务
        $.ajax({
            url: "http://127.0.0.1:5000/run/",
            type: "GET",
            success: function (response) {
                set_progress_rate(100, 100);
                console.log('执行完成,结果为:' + response['results']);
            }
        });
    });
    setInterval(function () {
        //每1秒请求一次进度
        $.ajax({
            url: "http://127.0.0.1:5000/progress/",
            type: "GET",
            success: function (response) {
                console.log(response);
                var n = response["n"];
                var total = response["total"];
                set_progress_rate(n, total);
            }
        });
    }, 1000);
</script>
</html>

效果

Python异步处理返回进度——使用Flask实现进度条

Flask使用简单异步任务

在Flask中使用简单异步任务最简洁优雅的原生实现:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from flask import Flask
from time import sleep
from concurrent.futures import ThreadPoolExecutor
# DOCS https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor
executor = 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()

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://xercis.blog.csdn.net/article/details/121920979

延伸 · 阅读

精彩推荐
  • Python各个系统下的Python解释器相关安装方法

    各个系统下的Python解释器相关安装方法

    这篇文章主要介绍了各个系统下的Python解释器相关安装方法,Python在很多Linux发行版中已经被默认安装,需要的朋友可以参考下...

    脚本之家4692020-07-31
  • Pythonpython写入并获取剪切板内容的实例

    python写入并获取剪切板内容的实例

    今天小编就为大家分享一篇python写入并获取剪切板内容的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    泛舟流年11202021-02-27
  • Python彻底吃透理解Python基础33个关键字详细教程

    彻底吃透理解Python基础33个关键字详细教程

    这篇文章主要为大家介绍了彻底吃透理解Python中33个关键字的详细教程,有需要打好Python基础的同学可以借鉴参考下,希望能成为您成功路上的一块垫脚石...

    数据分析与统计学之美5402022-02-19
  • PythonPython根据文件名批量转移图片的方法

    Python根据文件名批量转移图片的方法

    今天小编就为大家分享一篇Python根据文件名批量转移图片的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Chris_zhangrx10522021-04-10
  • Python对python 通过ssh访问数据库的实例详解

    对python 通过ssh访问数据库的实例详解

    今天小编就为大家分享一篇对python 通过ssh访问数据库的实例详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Howard93wws11022021-05-30
  • PythonPython常用知识点汇总

    Python常用知识点汇总

    这篇文章主要介绍了Python常用知识点汇总,包括Set集合,函数,深入拷贝,浅入拷贝,需要的朋友可以参考下 ...

    脚本之家10462020-08-22
  • Pythonpython验证身份证信息实例代码

    python验证身份证信息实例代码

    这篇文章主要介绍了python验证身份证信息的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面...

    Lucas__liu4472021-06-24
  • Python使用python遍历指定城市的一周气温

    使用python遍历指定城市的一周气温

    本文主要介绍了使用python遍历指定城市的一周气温的实现方法。具有很好的参考价值,下面跟着小编一起来看下吧...

    yrsss5582020-09-27