From 65a2b6a223f8ee44335c66f9171eaa9428e9bf46 Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 15 Oct 2024 22:30:15 +0000 Subject: [PATCH] u2c: fix excessive FDs it would open separate FDs for all chunks to be uploaded... open and close files as they are needed during upload instead --- bin/u2c.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/bin/u2c.py b/bin/u2c.py index 0f88c085..e037f2ec 100755 --- a/bin/u2c.py +++ b/bin/u2c.py @@ -270,7 +270,16 @@ class FileSlice(object): self.len = tlen self.cdr = self.car + self.len self.ofs = 0 # type: int - self.f = open(file.abs, "rb", 512 * 1024) + + self.f = None + self.seek = self._seek0 + self.read = self._read0 + + def _open(self): + self.seek = self._seek + self.read = self._read + + self.f = open(self.file.abs, "rb", 512 * 1024) self.f.seek(self.car) # https://stackoverflow.com/questions/4359495/what-is-exactly-a-file-like-object-in-python @@ -282,10 +291,15 @@ class FileSlice(object): except: pass # py27 probably + def close(self, *a, **ka): + return # until _open + def tell(self): return self.ofs - def seek(self, ofs, wh=0): + def _seek(self, ofs, wh=0): + assert self.f # !rm + if wh == 1: ofs = self.ofs + ofs elif wh == 2: @@ -299,12 +313,22 @@ class FileSlice(object): self.ofs = ofs self.f.seek(self.car + ofs) - def read(self, sz): + def _read(self, sz): + assert self.f # !rm + sz = min(sz, self.len - self.ofs) ret = self.f.read(sz) self.ofs += len(ret) return ret + def _seek0(self, ofs, wh=0): + self._open() + return self.seek(ofs, wh) + + def _read0(self, sz): + self._open() + return self.read(sz) + class MTHash(object): def __init__(self, cores):