Skip to content

Instantly share code, notes, and snippets.

@depfryer
Created October 25, 2021 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save depfryer/782dad1b0ba672cff4026e4a8bfe73ee to your computer and use it in GitHub Desktop.
Save depfryer/782dad1b0ba672cff4026e4a8bfe73ee to your computer and use it in GitHub Desktop.
python stream server exemple
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())
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)
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