Nicer transfer queue iteration

This commit is contained in:
Sybren A. Stüvel 2018-03-09 16:30:17 +01:00
parent ef5c46eba5
commit a2ca66a2f6
2 changed files with 13 additions and 21 deletions

View File

@ -27,14 +27,7 @@ class FileCopier(threading.Thread, transfer.FileTransferer):
transfer.Action.MOVE: shutil.move,
}
while True:
try:
src, dst, act = self.pop_queued()
except self.Done:
break
except self.Empty:
continue
for src, dst, act in self.iter_queue():
try:
if dst.exists():
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
# be reported there.
self.queue.put((src, dst, act))
return
break
if files_transferred:
log.info('Transferred %d files', files_transferred)

View File

@ -78,20 +78,19 @@ class FileTransferer(metaclass=abc.ABCMeta):
"%d files couldn't be transferred" % len(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.
:raises Empty: when the queue is empty, but more files may be queued
in the future.
"""
def iter_queue(self) -> typing.Iterable[QueueItem]:
"""Generator, yield queued items until the work is done."""
while True:
if self.abort.is_set():
return
try:
return self.queue.get(timeout=0.1)
yield self.queue.get(timeout=0.1)
except queue.Empty:
if self.done.is_set():
raise self.Done()
raise self.Empty()
return
@abc.abstractmethod
def start(self) -> None: