python3写一个异步流式 http 接口服务调用大模型(async, stream, sanic)---6.2

python3写一个异步流式 http 接口服务调用大模型(async, stream, sanic)

python3写一个异步流式 http 接口服务调用大模型(async, stream, sanic)

python3写一个异步流式 http 接口服务调用大模型(async, stream, sanic),异步适合处理I/O密集型操作(文件/网络请求),特别地,调用大模型等待时间特别长。记录一下!

一、Sanic介绍

Sanic是一个Python3的web服务器和web框架,旨在快速运行。它允许使用Python 3.5中添加的async/await语法。

安装

pip install sanic

二、异步接口代码实现

# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time    : 2025/07/19 18:34
# @author  : Mo
# @function: async http
import json
import traceback
import time
import sys
import os

path_sys = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
sys.path.append(path_sys)
print(path_sys)

from sanic.views import HTTPMethodView
from sanic.log import logger
from sanic import response
from sanic import Sanic
import asyncio


app = Sanic("add_async")
app.config.RESPONSE_TIMEOUT = 96   # 超时时间配置
rsp_kwargs = {
            "content_type": "text/event-stream",
            "headers": {
                "Cache-Control": "no-cache",
                "Connection": "keep-alive",
                "X-Accel-Buffering": "no"
            }
        }

async def n_range_stream(n=None, **kwargs):
    """  异步程序xxx   """
    await asyncio.sleep(1)
    n = n or 10
    for i in range(n):
        await asyncio.sleep(0.01)
        yield json.dumps({"index": i}, ensure_ascii=False) + "\n"


class textStreamView(HTTPMethodView):
    def __init__(self) -> None:
        super().__init__()

    async def post(self, request):
        """   简单流式输出  """
        if "application/json" in request.content_type:
            data = request.json
        else:
            data = request.form

        async def stream_generator(response):
            # 调用异步流式响应函数
            async for content in n_range_stream(**data):
                # 将每个响应块发送给客户端
                # await asyncio.sleep(0.01)
                await response.write(content)

        # 返回流式响应
        return response.ResponseStream(stream_generator, **rsp_kwargs)


app.add_route(textStreamView.as_view(),
              uri="/text_stream",
              methods=["POST", "GET"],
              version=1,
              name="add")

# ### 测试异步, async
# import time
# time_start = time.time()
# resp = asyncio.run(n_range_stream(a=2, b=3))
# print(resp)
# time_end = time.time()
# print(time_end - time_start)


if __name__ == '__main__':
    app.run(host="0.0.0.0",
            port=8032,
            workers=1,
            access_log=True
            )

接口访问

接口: http://127.0.0.1:8032/v1/text_stream
入参:

{"n": 10}

在这里插入图片描述

参考

希望对你有所帮助!

[外链图片转存中…(img-ZcI2LCY1-1753262174740)]

参考

希望对你有所帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值