From 7262e8415067145f9af6e2920c829cf9c3245c75 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 18 Jul 2017 04:30:21 -0300 Subject: [PATCH] use a buffer to wait for blob completion --- lbrynet/core/HashBlob.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lbrynet/core/HashBlob.py b/lbrynet/core/HashBlob.py index d80cede96..4b51280b1 100644 --- a/lbrynet/core/HashBlob.py +++ b/lbrynet/core/HashBlob.py @@ -385,19 +385,21 @@ class BlobFileCreator(HashBlobCreator): def __init__(self, blob_dir): HashBlobCreator.__init__(self) self.blob_dir = blob_dir - self.out_file = tempfile.NamedTemporaryFile(delete=False, dir=self.blob_dir) + self.buffer = StringIO() def _close(self): - temp_file_name = self.out_file.name - self.out_file.close() if self.blob_hash is not None: - shutil.move(temp_file_name, os.path.join(self.blob_dir, self.blob_hash)) - else: - os.remove(temp_file_name) + def _twrite(data, blob_dir, blob_hash): + with open(os.path.join(blob_dir, blob_hash), 'w') as out_file: + out_file.write(data.getvalue()) + d = threads.deferToThread(_twrite, self.buffer, + self.blob_dir, self.blob_hash) + del self.buffer + return d return defer.succeed(True) def _write(self, data): - self.out_file.write(data) + self.buffer.write(data) class TempBlobCreator(HashBlobCreator):