Created
April 19, 2019 14:21
-
-
Save Sean-Bradley/e4e57324fa05a6defde3cbd7e70220bb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import psycopg2 | |
with requests.get("http://127.0.0.1:5000/very_large_request/100000000", stream=True) as r: | |
conn = psycopg2.connect(dbname="stream_test", | |
user="postgres", password="postgres") | |
cur = conn.cursor() | |
sql = "INSERT INTO transactions (txid, uid, amount) VALUES (%s, %s, %s)" | |
buffer = "" | |
for chunk in r.iter_content(chunk_size=1): | |
if chunk.endswith(b'\n'): | |
t = eval(buffer) | |
print(t) | |
cur.execute(sql, (t[0], t[1], t[2])) | |
conn.commit() | |
buffer = "" | |
else: | |
buffer += chunk.decode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment