use a buffer to wait for blob completion

This commit is contained in:
Victor Shyba 2017-07-18 04:30:21 -03:00 committed by Jack Robison
parent 5d69e74010
commit 7262e84150
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF

View file

@ -385,19 +385,21 @@ class BlobFileCreator(HashBlobCreator):
def __init__(self, blob_dir): def __init__(self, blob_dir):
HashBlobCreator.__init__(self) HashBlobCreator.__init__(self)
self.blob_dir = blob_dir self.blob_dir = blob_dir
self.out_file = tempfile.NamedTemporaryFile(delete=False, dir=self.blob_dir) self.buffer = StringIO()
def _close(self): def _close(self):
temp_file_name = self.out_file.name
self.out_file.close()
if self.blob_hash is not None: if self.blob_hash is not None:
shutil.move(temp_file_name, os.path.join(self.blob_dir, self.blob_hash)) def _twrite(data, blob_dir, blob_hash):
else: with open(os.path.join(blob_dir, blob_hash), 'w') as out_file:
os.remove(temp_file_name) 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) return defer.succeed(True)
def _write(self, data): def _write(self, data):
self.out_file.write(data) self.buffer.write(data)
class TempBlobCreator(HashBlobCreator): class TempBlobCreator(HashBlobCreator):