如何实现一个实时转发 Stream 流的接口 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
WindyXu
V2EX    Python

如何实现一个实时转发 Stream 流的接口

  •  
  •   WindyXu 2023-03-08 17:26:57 +08:00 2043 次点击
    这是一个创建于 1023 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想写一个实时转发 Stream 流内容的接口,不怎么懂 stream 流相关的知识

    之前的接口是

    async def index(path: str, request: Request): data = await request.json() resp = c.send_request(path, data) headers = dict(resp.headers) async def generate(): for chunk in resp.iter_lines(): if chunk: yield chunk + b'\n\n' return StreamingResponse(generate(), headers=headers) 

    问题是,这个会等接受完全部的 Stream 流信息,才会转发,想实现的效果是实时转发 stream 流信息,具体一点就是,接收到一行或者指定大小的信息块,就转发出去。

    想法是将 resp = c.send_request(path, data)这块改造成异步的,然后实时转发。

    目前的代码是

    async def index(path: str, request: Request): data = await request.json() async with httpx.AsyncClient() as client: async with client.stream('POST', path, json=data) as response: resp_headers = dict(response.headers) async def generate(): async for line in response.aiter_lines(): if line: yield line.encode('utf-8') + b'\n\n' return StreamingResponse(generate(), headers=resp_headers) 

    但是会报:

    httpx.StreamClosed: Attempted to read or stream content, but the stream has been closed.

    不知道怎么改,求赐教

    5 条回复    2023-03-09 09:47:50 +08:00
    yuanxing008
        1
    yuanxing008  
       2023-03-08 17:45:14 +08:00
    这个为什么还要走代码。直接用 Nginx 转出去不就好了么 你是要有业务逻辑处理?
    noparking188
        2
    noparking188  
       2023-03-08 17:50:30 +08:00
    有过类似需求,需要对下载过程中的 stream 数据进行处理,搜到篇文章 [How to stream Microsoft SQL Server to S3 using BCP on linux]( https://dstan.medium.com/streaming-microsoft-sql-server-to-s3-using-bcp-35241967d2e0),原理就是借助 named pipe 来对不支持 pipe 的下载工具截取 stream 进行处理。

    基于这个原理我写了个迁移 SQL Server 到 S3 的小工具: https://github.com/zhiweio/StreamXfer

    楼主可以参考下,读于你描述的需求,需要一个进程实时写 stream 到 named pipe ,另一个进程实时读 stream写到其它地方,类似:
    1. mkfifo /tmp/fifo
    2. wget 'https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-2021-financial-year-provisional/Download-data/annual-enterprise-survey-2021-financial-year-provisional-csv.csv' -o /tmp/fifo &
    3. cat /tmp/fifo | aws s3 cp - s3://bucket/xxx

    希望能够有帮助,如果可以的话欢迎给我一个 star ,谢谢 :)
    F281M6Dh8DXpD1g2
        3
    F281M6Dh8DXpD1g2  
       2023-03-08 17:51:52 +08:00
    找个用 splice api 的完事
    009694
        4
    009694  
       2023-03-08 18:55:38 +08:00 via iPhone
    不要用 async with 。这样 return 出去的时候连接就已经关了 。用 background 去处理 httpx 未关闭的连接
    ruanimal
        5
    ruanimal  
       2023-03-09 09:47:50 +08:00
    `data = await request.json()` 这里其实就取了全部数据了, 不存在实时转发吧
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1017 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 18:28 PVG 02:28 LAX 10:28 JFK 13:28
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86