TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How to return large response over an API?

2 pointsby cloverr20about 4 years ago
I am using a third party service which would transcribe an audio file and give me the entire transcription in a json format, I would read this and make a new json file by transforming it and send to s3. I am using python&#x2F;django for it along with json, requests and boto3 module, however I noticed as the application kept running the memory consumption kept increasing.<p>To fix this I had to write it as a streaming response to a json file and use ijson (https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;ijson&#x2F;) to read the content which decreased memory utilisation a lot. So while looking around, I found many people had the same issue, so this got me thinking how would I have sent this content if i had built the third party api myself.<p>Some previous questions asked on stackoverflow,<p>https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;2400643&#x2F;is-there-a-memory-efficient-and-fast-way-to-load-big-json-files-in-python<p>https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;11057712&#x2F;huge-memory-usage-of-pythons-json-module<p>How do you send a response via an api for very large content, along with their advantages&#x2F;disadvantages.

4 comments

aardvarkrabout 4 years ago
Sounds like this is a personal problem and not a them problem. They’re sending you a json and you’re complaining that <i>your</i> memory usage is exploding. That’s on you. If you’re not actively cleaning up after yourself idk how you can blame the <i>external</i> api for your <i>internal</i> memory issues. That’s my two cents at least. You can’t possibly design an api that fixes a user’s poor design.
评论 #27084042 未加载
tiew9Viiabout 4 years ago
If you can chunk the response json-seq&#x2F;xml (sax parser) may be worth looking at.<p>The server can incrementally stream a chunk and client incrementally consume a chunk keeping flat memory usage.<p>I think gRpc also supports streaming but don’t know much on it.<p>JSON is a bad format for large files as generally you need to read the entire file in to memory before you can use it as you observed.
评论 #27085185 未加载
pestatijeabout 4 years ago
Json is the default go-to standard for web services nowadays. But it doesn&#x27;t mean it is the best format for your requirements. I&#x27;m not sure what an audio transcription is exactly, but if you can &quot;stream&quot; it you don&#x27;t need json at all. Just use some basic serializer and stream that instead.
评论 #27087496 未加载
PEJOEabout 4 years ago
If you know the structure of the file so that you are comfortable reading it a bit at a time, and you’re confident the structure won’t change, C has several ways to control how much of the file you read in. You could just write a python module to handle this situation.