Time based approach for process_events_iter

This commit is contained in:
Philipp Wolfer
2020-10-07 15:21:47 +02:00
parent 8012d54a5b
commit 38cfe4255d
2 changed files with 28 additions and 10 deletions

View File

@@ -523,8 +523,15 @@ class Tagger(QtWidgets.QApplication):
new_files.sort(key=lambda x: x.filename)
self.window.set_sorting(False)
self._pending_files_count += len(new_files)
for file in process_events_iter(new_files):
for i, file in enumerate(new_files):
file.load(partial(self._file_loaded, target=target))
# Calling processEvents helps processing the _file_loaded
# callbacks in between, which keeps the UI more responsive.
# Avoid calling it to often to not slow down the loading to much
# Using an uneven number to have the unclustered file counter
# not look stuck in certain digits.
if i % 17 == 0:
QtCore.QCoreApplication.processEvents()
@staticmethod
def _scan_paths_recursive(paths, recursive, ignore_hidden):
@@ -820,7 +827,7 @@ class Tagger(QtWidgets.QApplication):
for name, artist, files in Cluster.cluster(files, 1.0, self):
cluster = self.load_cluster(name, artist)
cluster_files[cluster].extend(files)
for cluster, files in process_events_iter(cluster_files.items(), steps=2):
for cluster, files in process_events_iter(cluster_files.items()):
cluster.add_files(files)
self.window.set_sorting(True)

View File

@@ -47,7 +47,7 @@ from operator import attrgetter
import os
import re
import sys
from time import time
from time import monotonic
import unicodedata
from dateutil.parser import parse
@@ -88,17 +88,28 @@ class LockableObject(QtCore.QObject):
self.__lock.unlock()
def process_events_iter(iterable, steps=10):
def process_events_iter(iterable, interval=0.1):
"""
Creates an iterator over iterable that calls QCoreApplication.processEvents()
in certain iteration steps.
after certain time intervals.
This must only be used in the main thread.
Args:
iterable: iterable object to iterate over
interval: interval in seconds to call QCoreApplication.processEvents()
"""
for i, item in enumerate(iterable):
if (i + 1) % steps == 0:
QtCore.QCoreApplication.processEvents()
if interval:
start = monotonic()
for item in iterable:
if interval:
now = monotonic()
delta = now - start
if delta > interval:
start = now
QtCore.QCoreApplication.processEvents()
yield item
QtCore.QCoreApplication.processEvents()
_io_encoding = sys.getfilesystemencoding()
@@ -291,7 +302,7 @@ def throttle(interval):
def later():
mutex.lock()
func(*decorator.args, **decorator.kwargs)
decorator.prev = time()
decorator.prev = monotonic()
decorator.is_ticking = False
mutex.unlock()
@@ -303,7 +314,7 @@ def throttle(interval):
mutex.unlock()
return
mutex.lock()
now = time()
now = monotonic()
r = interval - (now-decorator.prev)*1000.0
if r <= 0:
func(*args, **kwargs)