Nicer transfer queue iteration
This commit is contained in:
parent
ef5c46eba5
commit
a2ca66a2f6
@ -27,14 +27,7 @@ class FileCopier(threading.Thread, transfer.FileTransferer):
|
|||||||
transfer.Action.MOVE: shutil.move,
|
transfer.Action.MOVE: shutil.move,
|
||||||
}
|
}
|
||||||
|
|
||||||
while True:
|
for src, dst, act in self.iter_queue():
|
||||||
try:
|
|
||||||
src, dst, act = self.pop_queued()
|
|
||||||
except self.Done:
|
|
||||||
break
|
|
||||||
except self.Empty:
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if dst.exists():
|
if dst.exists():
|
||||||
st_src = src.stat()
|
st_src = src.stat()
|
||||||
@ -64,7 +57,7 @@ class FileCopier(threading.Thread, transfer.FileTransferer):
|
|||||||
# copied. The one we just failed (due to this exception) should also
|
# copied. The one we just failed (due to this exception) should also
|
||||||
# be reported there.
|
# be reported there.
|
||||||
self.queue.put((src, dst, act))
|
self.queue.put((src, dst, act))
|
||||||
return
|
break
|
||||||
|
|
||||||
if files_transferred:
|
if files_transferred:
|
||||||
log.info('Transferred %d files', files_transferred)
|
log.info('Transferred %d files', files_transferred)
|
||||||
|
|||||||
@ -78,20 +78,19 @@ class FileTransferer(metaclass=abc.ABCMeta):
|
|||||||
"%d files couldn't be transferred" % len(files_remaining),
|
"%d files couldn't be transferred" % len(files_remaining),
|
||||||
files_remaining)
|
files_remaining)
|
||||||
|
|
||||||
def pop_queued(self) -> typing.Optional[QueueItem]:
|
|
||||||
"""Pops an item off the queue, waiting 0.1 sec if the queue is empty.
|
|
||||||
|
|
||||||
:raises Done: when all files have been handled, and the work is done.
|
def iter_queue(self) -> typing.Iterable[QueueItem]:
|
||||||
:raises Empty: when the queue is empty, but more files may be queued
|
"""Generator, yield queued items until the work is done."""
|
||||||
in the future.
|
|
||||||
"""
|
|
||||||
|
|
||||||
try:
|
while True:
|
||||||
return self.queue.get(timeout=0.1)
|
if self.abort.is_set():
|
||||||
except queue.Empty:
|
return
|
||||||
if self.done.is_set():
|
|
||||||
raise self.Done()
|
try:
|
||||||
raise self.Empty()
|
yield self.queue.get(timeout=0.1)
|
||||||
|
except queue.Empty:
|
||||||
|
if self.done.is_set():
|
||||||
|
return
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user