Do slow price discovery in another thread to speed up startup time and responsiveness.
authorAmir Taaki <genjix@riseup.net>
Mon, 2 Jul 2012 20:39:45 +0000 (22:39 +0200)
committerAmir Taaki <genjix@riseup.net>
Mon, 2 Jul 2012 20:39:45 +0000 (22:39 +0200)
lib/exchange_rate.py
lib/gui_lite.py

index 735ac9d..e8c9fac 100644 (file)
@@ -1,19 +1,31 @@
+from PyQt4.QtCore import SIGNAL
 import decimal
 import httplib
 import json
+import threading
 
-class Exchanger:
+class Exchanger(threading.Thread):
 
-    def __init__(self, quote_currencies, refresh_balance):
-        self.refresh_balance = refresh_balance
+    def __init__(self, parent):
+        threading.Thread.__init__(self)
+        self.daemon = True
+        self.parent = parent
         self.quote_currencies = None
+        self.lock = threading.Lock()
+        # Do price discovery
+        self.start()
 
     def exchange(self, btc_amount, quote_currency):
-        if self.quote_currencies is None:
+        with self.lock:
+            if self.quote_currencies is None:
+                return None
+            quote_currencies = self.quote_currencies.copy()
+        if quote_currency not in quote_currencies:
             return None
-        if quote_currency not in self.quote_currencies:
-            return None
-        return btc_amount * self.quote_currencies[quote_currency]
+        return btc_amount * quote_currencies[quote_currency]
+
+    def run(self):
+        self.discovery()
 
     def discovery(self):
         connection = httplib.HTTPSConnection('intersango.com')
@@ -26,12 +38,14 @@ class Exchanger:
         # 2 = BTC:EUR
         # 3 = BTC:USD
         # 4 = BTC:PLN
-        self.quote_currencies = {}
+        quote_currencies = {}
         try:
-            self.quote_currencies["GBP"] = self.lookup_rate(response, 1)
-            self.quote_currencies["EUR"] = self.lookup_rate(response, 2)
-            self.quote_currencies["USD"] = self.lookup_rate(response, 3)
-            self.refresh_balance()
+            quote_currencies["GBP"] = self.lookup_rate(response, 1)
+            quote_currencies["EUR"] = self.lookup_rate(response, 2)
+            quote_currencies["USD"] = self.lookup_rate(response, 3)
+            with self.lock:
+                self.quote_currencies = quote_currencies
+            self.parent.emit(SIGNAL("refresh_balance()"))
         except KeyError:
             pass
 
index bf5454c..0d23c07 100644 (file)
@@ -105,9 +105,10 @@ class MiniWindow(QDialog):
 
         self.btc_balance = 0
         self.quote_currencies = ("EUR", "USD", "GBP")
-        self.exchanger = exchange_rate.Exchanger(self.quote_currencies,
-                                                 self.refresh_balance)
-        QTimer.singleShot(1000, self.exchanger.discovery)
+        self.exchanger = exchange_rate.Exchanger(self)
+        # Needed because price discovery is done in a different thread
+        # which needs to be sent back to this main one to update the GUI
+        self.connect(self, SIGNAL("refresh_balance()"), self.refresh_balance)
 
         self.balance_label = BalanceLabel(self.change_quote_currency)
         self.balance_label.setObjectName("balance_label")