Skip to content

Commit 64978df

Browse files
committed
MOD: Define a chunk size for requests streaming
1 parent 6f2671d commit 64978df

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#### Bug fixes
1010
- Fixed an issue where `batch.download` and `batch.download_async` would fail if requested files already existed in the output directory
11+
- Fixed an issue where `batch.download`, `batch.download_async`, and `timeseries.get_range` could use a lot of memory while streaming data
1112

1213
## 0.33.0 - 2024-04-16
1314

databento/common/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
x[0]: np.iinfo(x[1]).max for x in InstrumentDefMsg._dtypes if not isinstance(x[1], str)
2626
}
2727

28+
HTTP_STREAMING_READ_SIZE: Final = 2**12
29+
2830
SCHEMA_STRUCT_MAP: Final[dict[Schema, type[DBNRecord]]] = {
2931
Schema.DEFINITION: InstrumentDefMsg,
3032
Schema.IMBALANCE: ImbalanceMsg,

databento/historical/api/batch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from databento_dbn import SType
2525
from requests.auth import HTTPBasicAuth
2626

27+
from databento.common.constants import HTTP_STREAMING_READ_SIZE
2728
from databento.common.enums import Delivery
2829
from databento.common.enums import Packaging
2930
from databento.common.enums import SplitDuration
@@ -416,7 +417,7 @@ def _download_batch_file(
416417
) as response:
417418
check_http_error(response)
418419
with open(output_path, mode=mode) as f:
419-
for chunk in response.iter_content(chunk_size=None):
420+
for chunk in response.iter_content(chunk_size=HTTP_STREAMING_READ_SIZE):
420421
f.write(chunk)
421422
except BentoHttpError as exc:
422423
if exc.http_status == 429:

databento/historical/http.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from os import PathLike
99
from typing import IO
1010
from typing import Any
11+
from typing import Final
1112

1213
import aiohttp
1314
import requests
@@ -16,6 +17,7 @@
1617
from requests import Response
1718
from requests.auth import HTTPBasicAuth
1819

20+
from databento.common.constants import HTTP_STREAMING_READ_SIZE
1921
from databento.common.dbnstore import DBNStore
2022
from databento.common.error import BentoClientError
2123
from databento.common.error import BentoDeprecationWarning
@@ -25,7 +27,7 @@
2527
from databento.common.system import USER_AGENT
2628

2729

28-
WARNING_HEADER_FIELD: str = "X-Warning"
30+
WARNING_HEADER_FIELD: Final = "X-Warning"
2931

3032

3133
class BentoHttpAPI:
@@ -137,7 +139,7 @@ def _stream(
137139
writer = open(path, "x+b")
138140

139141
try:
140-
for chunk in response.iter_content(chunk_size=None):
142+
for chunk in response.iter_content(chunk_size=HTTP_STREAMING_READ_SIZE):
141143
writer.write(chunk)
142144
except Exception as exc:
143145
raise BentoError(f"Error streaming response: {exc}") from None

0 commit comments

Comments
 (0)