From a00aabfa0d88ef239484cde8535c13ad77017955 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 4 Nov 2022 13:47:32 -0500 Subject: [PATCH] PYTHON-3502 GridFSBucket.download_to_stream slow (#1108) --- doc/changelog.rst | 8 ++++++++ gridfs/__init__.py | 10 ++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index 4688a8fb6..4f4e5ace7 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,6 +1,14 @@ Changelog ========= +Changes in Version 4.3.3 +------------------------ + +- Fixed a performance regression in :meth:`~gridfs.GridOut.download_to_stream` + and :meth:`~gridfs.GridOut.download_to_stream_by_name` by reading in chunks + instead of line by line. + + Changes in Version 4.3 (4.3.2) ------------------------------ diff --git a/gridfs/__init__.py b/gridfs/__init__.py index 6ab843a85..692567b2d 100644 --- a/gridfs/__init__.py +++ b/gridfs/__init__.py @@ -796,7 +796,10 @@ class GridFSBucket(object): Added ``session`` parameter. """ with self.open_download_stream(file_id, session=session) as gout: - for chunk in gout: + while True: + chunk = gout.readchunk() + if not len(chunk): + break destination.write(chunk) @_csot.apply @@ -977,7 +980,10 @@ class GridFSBucket(object): Added ``session`` parameter. """ with self.open_download_stream_by_name(filename, revision, session=session) as gout: - for chunk in gout: + while True: + chunk = gout.readchunk() + if not len(chunk): + break destination.write(chunk) def rename(