zoukankan      html  css  js  c++  java
  • Python Flask后端异步处理(一)

      Flask是Python中有名的轻量级同步Web框架,但是在实际的开发中,可能会遇到需要长时间处理的任务,此时就需要使用异步的方式来实现,让长时间任务在后台运行,先将本次请求的相应状态返回给前端,不让前端界面卡顿。

      在碎遮扫描系统的编写中,当对目标进行全方位扫描时,扫描这个动作是需要长时间处理的。因为现在还没有加快每个部分的扫描速度,所以想要将一个目标扫描完大概需要五到十分钟的时间,所以不可能让用户一直等待这么长时间的页面刷新,需要将任务放在后台执行,先给用户返回正常的界面。

      本文介绍Python原生的方式,即多线程或者多进程来进行耗时任务的执行,实现后端的异步处理。这样也很好理解,直接暴力开启一个新的线程/进程来进行耗时任务的执行,主线程/进程用来返回正常前端界面给用户。

      通过多线程来实现

    import time
    from concurrent.futures import ThreadPoolExecutor
    executor = ThreadPoolExecutor()
    #executor = ThreadPoolExecutor(10)里面的数字是线程池所能同时进行的最大数量
    
    def run():
        time.sleep(10)
        print("耗时任务执行结束")
    
    @app.route('/test')
    def test():
        # 交给线程去处理耗时任务
        executor.submit(run)
        return "cheer!"

      通过多进程来实现:

    import time
    from concurrent.futures import ProcessPoolExecutor
    
    executor = ProcessPoolExecutor()
    #不限制数量的话最大进程数即为电脑CPU的核数
    
    @app.route('/test_console', methods=['GET', 'POST'])
    @login_required
    def console():
        bugbit, bugtype = core.GetBit()
        counts = core.GetCounts()
        ports = core.GetPort()
        services = core.GetServices()
        target = core.GetTargetCount()
        try:
            lastscantime = BaseInfo.query.order_by(BaseInfo.id.desc()).first().date
        except:
            lastscantime = "暂无扫描"
            pass
        if request.method == 'GET':
            return render_template('console.html', bugbit=bugbit, bugtype=bugtype, counts=counts, lastscantime=lastscantime,
                                   ports=ports, services=services, target=target)
        else:
            urls = request.form.get('urls')
            urls = urls.split()
            print(urls)
            for url in urls:
                redispool.hincrby('targetscan', 'waitcount', 1)
            executor.submit(SZheConsole, urls)
            target = core.GetTargetCount()
            return render_template('console.html', bugbit=bugbit, bugtype=bugtype, counts=counts, lastscantime=lastscantime,ports=ports, services=services, target=target)
    

      这里是碎遮扫描系统目前后端异步的多进程实现,之前时间匆忙所以实现的很简陋哈哈哈,今天准备弃掉原生的后端异步实现了,重构一下扫描系统的后端异步。

      其中

    executor.submit(SZheConsole, urls)

      就是使用SZheConsole函数对输入的扫描目标进行全方位目标检测,在这里开启了一个新的进程执行扫描,接着就返回了前端界面,让用户能够正常看到界面。

    return render_template('console.html', bugbit=bugbit, bugtype=bugtype, counts=counts, lastscantime=lastscantime,
                                   ports=ports, services=services, target=target)

      虽然这样的原生实现异步处理很简单快捷,但是不足的地方在扫描系统的使用过程中已经有很多师傅提出来了:没有扫描的进度条显示,不能对扫描任务进行暂停,如果有个地方卡死了,就只能一直卡在那里,而不能进行丢弃等等,所以需要使用已存在的异步框架来优化异步处理:D

      请听下回分解。咕咕咕

    参考链接:

  • 相关阅读:
    IOS获取senderTitle
    IOSSegmentedControl(添加到Nav上)的简单使用方法
    MinaService
    java nio(reactor/selector)
    Java,反射机制
    http, get, post, HttpClient
    java.util.ConcurrentModificationException
    愛上一個不該 愛的人
    how to be a good ladyprogrammer?
    JQuery(那个夏日的午后)
  • 原文地址:https://www.cnblogs.com/Cl0ud/p/13191526.html
Copyright © 2011-2022 走看看