python flask线程池用法

flask线程池用法

1.线程池的用法
  1. 在写任务调度的时候,难免遇到使用多线程、多进程、线程池、进程池的场景 ,
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():
 # 通过submit函数提交执行的函数到线程池中,submit函数立即返回,不阻塞
 executor.submit(long_task, 'hello', 123)
 return 'long task running.'

def long_task(arg1, arg2):
 print("args: %s %s!" % (arg1, arg2))
 sleep(5)
 print("Task is done!")

if __name__ == '__main__':
 app.run()
2.thread的用法
import time
from threading import Thread

def async_fun(f):

    def inner_fun(*args, **kwargs):

        t = Thread(target=f, args=args, kwargs=kwargs)

        t.start()

    return inner_fun


@async_fun

def test_a():

    time.sleep(10)

    print("test a run")


def test_b():

    test_a()

    print("test b run")

test_b()
3.flask开启多线程支持

1)threaded : 多线程支持,默认为False,即不开启多线程;

app.run(threaded=True)

2)processes:进程数量,默认为1.

app.run(processes=True)

ps:多进程或多线程只能选择一个,不能同时开启

使用示例:

app.run(host=myaddr,port=myport,debug=False,threaded=True) ### threaded开启以后 不需要等队列 threaded=True
    #或者
    #app.run(host=myaddr,port=myport,debug=False,processes=3) ### processes=N 进程数量,默认为1个

3.设置守护线程

import time
import threading

def test():
    while True:
        print threading.currentThread()
        time.sleep(1)

if __name__ == '__main__':
    t1 = threading.Thread(target=test)
    t1.setDaemon(True)  # python2.7设置
    t1.start()  
----------------------------------------------------------
# python 3.7实现
t1 = threading.Thread(target=ppp, args=(), daemon=True)
t1.start()
相关链接

https://www.cxyzjd.com/article/xiaoyu_wu/102820384

https://www.jb51.net/article/212169.htm

https://cloud.tencent.com/developer/article/1572261

https://www.cxyzjd.com/article/qq_33682575/105107041

https://python-parallel-programmning-cookbook.readthedocs.io/zh_CN/latest/chapter4/02_Using_the_concurrent.futures_Python_modules.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Flask 蓝图是一种组织 Flask 应用程序的有效方式。它允许您将应用程序分解为可重用的模块,并使应用程序更易于管理和扩展。蓝图可以定义路由、视图、静态文件和模板等 Flask 应用程序中的各种组件。 以下是 Python Flask 蓝图的用法: 1. 创建蓝图 要创建蓝图,您需要使用 Flask 实例的 Blueprint() 函数。Blueprint() 函数需要两个参数:蓝图名称和蓝图文件的位置。 例如,以下代码创建了一个名为 'main' 的蓝图,并将其保存在 main.py 文件中: ``` from flask import Blueprint main_blueprint = Blueprint('main', __name__) ``` 2. 定义路由 要在蓝图中定义路由,您需要使用 Blueprint 对象的 route() 方法。该方法与 Flask 实例的 route() 方法类似。 例如,以下代码在 'main' 蓝图中定义了一个路由: ``` @main_blueprint.route('/') def index(): return 'Hello, World!' ``` 3. 注册蓝图 要在 Flask 应用程序中使用蓝图,您需要将其注册到应用程序中。可以使用 Flask 实例的 register_blueprint() 方法将蓝图注册到应用程序中。 例如,以下代码将 'main' 蓝图注册到 Flask 应用程序中: ``` from flask import Flask from main import main_blueprint app = Flask(__name__) app.register_blueprint(main_blueprint) ``` 4. 使用蓝图的静态文件和模板 蓝图也可以定义其自己的静态文件和模板。要使用蓝图的静态文件和模板,您需要在蓝图的文件夹中创建一个名为 'static' 和 'templates' 的文件夹,并将静态文件和模板放在这些文件夹中。 例如,以下代码显示了如何在蓝图中使用静态文件和模板: ``` main_blueprint = Blueprint('main', __name__, template_folder='templates', static_folder='static') @main_blueprint.route('/') def index(): return render_template('index.html') ``` 以上就是 Python Flask 蓝图的用法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值