[gnome-music/wip/rate-limit] grilo: rate-limit content-changed signal



commit c4f1b34fece8c1f6ef2750f2d059019b0f952778
Author: Vadim Rutkovsky <vrutkovs redhat com>
Date:   Tue Jun 16 17:22:13 2015 +0200

    grilo: rate-limit content-changed signal

 gnomemusic/__init__.py |   21 +++++++++++++++++++++
 gnomemusic/grilo.py    |    3 ++-
 2 files changed, 23 insertions(+), 1 deletions(-)
---
diff --git a/gnomemusic/__init__.py b/gnomemusic/__init__.py
index a69b937..aa83295 100644
--- a/gnomemusic/__init__.py
+++ b/gnomemusic/__init__.py
@@ -28,6 +28,7 @@
 from gi.repository import Tracker
 from itertools import chain
 import logging
+import time
 logger = logging.getLogger(__name__)
 tabbing = 0
 
@@ -52,6 +53,26 @@ def log(fn):
     return wrapped
 
 
+def RateLimited(maxPerSecond):
+    minInterval = 1.0 / float(maxPerSecond)
+
+    def decorate(func):
+        lastTimeCalled = [0.0]
+
+        def rateLimitedFunction(*args, **kargs):
+            elapsed = time.clock() - lastTimeCalled[0]
+            leftToWait = minInterval - elapsed
+            if leftToWait > 0:
+                time.sleep(leftToWait)
+            ret = func(*args, **kargs)
+            lastTimeCalled[0] = time.clock()
+            return ret
+
+        return rateLimitedFunction
+
+    return decorate
+
+
 class TrackerWrapper:
     class __TrackerWrapper:
         def __init__(self):
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index 2da0e04..dac1ab6 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -26,7 +26,7 @@
 # delete this exception statement from your version.
 from gi.repository import GLib, GObject
 from gnomemusic.query import Query
-from gnomemusic import log, TrackerWrapper
+from gnomemusic import log, RateLimited, TrackerWrapper
 import logging
 import os
 os.environ['GRL_PLUGIN_RANKS'] = 'local-metadata:3,filesystem:2,tracker:1,lastfm-albumart:0'
@@ -98,6 +98,7 @@ class Grilo(GObject.GObject):
             logger.debug("tracker found")
 
     @log
+    @RateLimited(2)
     def _on_content_changed(self, mediaSource, changedMedias, changeType, locationUnknown):
         try:
             with self.tracker.handler_block(self.notification_handler):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]