save using a FBP instead of thread

This commit is contained in:
Victor Shyba 2017-07-24 04:04:15 -03:00 committed by Jack Robison
parent a3e5c61b50
commit a6b8327fcf
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF

View file

@ -1,4 +1,4 @@
from StringIO import StringIO from io import BytesIO, StringIO
import logging import logging
import os import os
import tempfile import tempfile
@ -6,6 +6,7 @@ import threading
import shutil import shutil
from twisted.internet import interfaces, defer, threads from twisted.internet import interfaces, defer, threads
from twisted.protocols.basic import FileSender from twisted.protocols.basic import FileSender
from twisted.web.client import FileBodyProducer
from twisted.python.failure import Failure from twisted.python.failure import Failure
from zope.interface import implements from zope.interface import implements
from lbrynet import conf from lbrynet import conf
@ -385,17 +386,14 @@ 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.buffer = StringIO() self.buffer = BytesIO()
def _close(self): def _close(self):
if self.blob_hash is not None: if self.blob_hash is not None:
def _twrite(data, blob_dir, blob_hash): self.buffer.seek(0)
with open(os.path.join(blob_dir, blob_hash), 'w') as out_file: out_path = os.path.join(self.blob_dir, self.blob_hash)
out_file.write(data.getvalue()) producer = FileBodyProducer(self.buffer)
d = threads.deferToThread(_twrite, self.buffer, return producer.startProducing(open(out_path, 'wb'))
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):