python蓝图、拦截器、异常处理、redis队列、线程池、控制台及日志文件输出

python蓝图、python拦截器、python统一异常处理、redis阻塞队列及redis连接池、线程池案例、控制台&日志文件输出

python 复制代码
from flask import Flask, request, jsonify
from pythonFlask.views.account import ac
from pythonFlask.views.order import order
import sys
import threading
import logging
import time
import redis
from redis.connection import ConnectionPool
import uuid
from concurrent.futures import ThreadPoolExecutor

REDIS_CONFIG = {
    "max_connections": 20,  # 连接池最大连接数
    "host": "XXXXXXX",
    "port": 6379,
    "db": 1,
    "password": "XXXXXX",  # 有密码填字符串,无则None
    "decode_responses": True,  # 自动bytes转str,不用手动decode
    "socket_timeout": None,  # 连接超时
    "retry_on_timeout": True,
    "socket_keepalive": True
}

# redis客户端
redisClient = redis.Redis(connection_pool=ConnectionPool(**REDIS_CONFIG))

# 日志
logging.basicConfig(filename="info.log", level=logging.INFO, encoding="UTF-8", filemode="w")
# 新增控制台输出
console = logging.StreamHandler(sys.stdout)
console.setLevel(logging.INFO)
logging.getLogger().addHandler(console)

TASK_QUEUE_NAME = "task_queue"


# @app.before_request  或者 app.before_request(auth)
def auth():
    logging.info(f"url:{request.path}")
    token = request.headers.get("token")
    print(request.path)
    print("读取token:", token)
    print("读取环境:", request.headers,
          type(request.headers))  # <class 'werkzeug.datastructures.headers.EnvironHeaders'>

    if request.method == "OPTIONS":  #
        return  # return代表方法结束,不拦截

    if request.path.startswith("/api/user/login"):
        return  # return代表方法结束,不拦截

    if not token:
        # 如何拦截?直接抛异常
        raise Exception("need login first")  # 抛出异常中断程序


###############################主应用开始########################################
app = Flask(__name__)


@app.errorhandler(500)
def internal_error(error):
    exc_type, exc_value, exc_traceback = sys.exc_info()
    # 安全获取异常信息
    if exc_value.args:
        message = str(exc_value.args[0])
    else:
        message = "服务器内部错误,请联系管理员"
    error_dict = {
        'success': False,
        'message': message,
        'data': None
    }
    logging.error(f"error:{error}")
    logging.error(f"error_message::{message}")
    return jsonify(error_dict), 500  # 如果你只返回 json、状态码依旧是 200。这里多返回一个500客户端本次请求结果:服务器内部错误


# 注册蓝图
app.register_blueprint(ac, url_prefix="/api/user")
app.register_blueprint(order, url_prefix="/api/order")
# 注册拦截器
app.before_request(auth)  # 这行代码等价@app.before_request


# 注册统一错误 处理
# app.errorhandle
# r(500)(internal_error) 已通过@app.errorhandler(500)包装
###############################主应用结束########################################


def publish(name):
    logging.info(name)
    flag = True
    while True:
        if flag:
            # 初始化塞入10条数据
            logging.info("一次性塞入10条订单")
            for x in range(10):
                redisClient.lpush(TASK_QUEUE_NAME, str(uuid.uuid4()))
            flag = False
        time.sleep(2)
        # 每2秒往redis队列塞一条
        new_order = str(uuid.uuid4())
        logging.info(f"【{threading.currentThread().getName()}】,生成新订单:{new_order}")
        redisClient.lpush(TASK_QUEUE_NAME, new_order)

def task_execute_function(name, task_uid):
    logging.info(f"【线程:{threading.currentThread().getName()}】,{name}开始处理订单:{task_uid}")
    # 在这里写真实订单业务逻辑
    time.sleep(3)
    logging.info(f"【线程:{threading.currentThread().getName()}】,{name}完成处理订单:{task_uid}")

def worker(name):
    # ✅ 线程池只创建一次!放到循环外面
    # 线程池
    # from concurrent.futures import ThreadPoolExecutor
    thread_pool = ThreadPoolExecutor(max_workers=10, thread_name_prefix="order_thread")
    try:
        while True:
            #queuename, task_str
            _, task_uuid_str = redisClient.brpop([TASK_QUEUE_NAME])
            # 提交任务到线程池并发执行
            thread_pool.submit(task_execute_function, name, task_uuid_str)
    finally:
        # 程序退出时优雅关闭线程池
        #wait = Ture:当前调用 shutdown 的线程阻塞等待,线程池中已经提交、正在运行的任务全部执行完毕
        thread_pool.shutdown(wait=True)

if __name__ == '__main__':
    # 启动一个守护线程,不断的读取队列
    threading.Thread(target=publish, args=("订单队列启动!",), daemon=True).start()
    threading.Thread(target=worker, args=("订单处理者",), daemon=True).start()
    redisClient.delete(TASK_QUEUE_NAME)
    app.run()
python 复制代码
#包pythonFlask.views.account.py
from flask import Blueprint
ac = Blueprint("account",__name__)
@ac.route("/login", methods=["get","POST"])
def login():
    return "登录"


#包pythonFlask.views.order.py
from flask import Blueprint
order = Blueprint("order",__name__)
@order.route("/order_list")
def order_list():
    return "订单列表"
相关推荐
喝茶与编码2 小时前
真实业务场景:高并发 Upsert 死锁频发?InnoDB 锁机制与重试机制深度解析
数据库·python
倒流时光三十年2 小时前
第一阶段 05 · Java 客户端查询类详解(Query / SearchCriteria / Response 与复杂拼接)
java·开发语言·python
CTA终结者2 小时前
近期AI量化学习,把规则改写接到策略开发
人工智能·python
有Li2 小时前
使用整合电子健康记录的大语言模型智能体实现前列腺癌患者教育个性化文献速递/医学智能体前沿
人工智能·python·机器学习·语言模型·医学生
米码收割机2 小时前
【Python】Flask+SQLite_web 宠物领养系统 (源码+文档)【独一无二】
前端·python·flask
卷无止境3 小时前
python进阶:类方法与静态方法的分野,classmethod 和 staticmethod 到底该怎么选
后端·python
卷无止境3 小时前
继承与多态:Python OOP里最容易被用错的两把梯子
后端·python
SelectDB技术团队3 小时前
丰巢日志平台 ELK 替代:Apache Doris / SelectDB 的技术能力与实践
开发语言·python