Created
October 25, 2021 10:19
-
-
Save depfryer/782dad1b0ba672cff4026e4a8bfe73ee to your computer and use it in GitHub Desktop.
python stream server exemple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import httpx | |
async def async_client(): | |
async with httpx.AsyncClient() as client: | |
async with client.stream('GET', 'http://127.0.0.1:5000/large.csv') as response: | |
async for chunk in response.aiter_text(): | |
print(chunk) | |
loop = asyncio.get_event_loop() | |
a = loop.run_until_complete(async_client()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
r = requests.get('http://127.0.0.1:5000/large.csv', stream=True) | |
if r.encoding is None: | |
r.encoding = 'utf-8' | |
for line in r.iter_lines(decode_unicode=True): | |
if line: | |
print(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.wrappers import Response | |
import time | |
app = Flask(__name__) | |
def iter_all_rows(): # generate fake wait and fake list of data | |
for i in range(100): | |
yield [str(i),str(i*i), str(i+i)] | |
time.sleep(1) | |
@app.route('/large.csv') | |
def generate_large_csv(): | |
def generate(): | |
for row in iter_all_rows(): | |
yield f"{','.join(row)}\n" | |
return app.response_class(generate()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment