Browse Source

Do slow price discovery in another thread to speed up startup time and responsiveness.

283
Amir Taaki 13 years ago
parent
commit
bd489e998f
  1. 34
      lib/exchange_rate.py
  2. 7
      lib/gui_lite.py

34
lib/exchange_rate.py

@ -1,19 +1,31 @@
from PyQt4.QtCore import SIGNAL
import decimal import decimal
import httplib import httplib
import json import json
import threading
class Exchanger: class Exchanger(threading.Thread):
def __init__(self, quote_currencies, refresh_balance): def __init__(self, parent):
self.refresh_balance = refresh_balance threading.Thread.__init__(self)
self.daemon = True
self.parent = parent
self.quote_currencies = None self.quote_currencies = None
self.lock = threading.Lock()
# Do price discovery
self.start()
def exchange(self, btc_amount, quote_currency): def exchange(self, btc_amount, quote_currency):
with self.lock:
if self.quote_currencies is None: if self.quote_currencies is None:
return None return None
if quote_currency not in self.quote_currencies: quote_currencies = self.quote_currencies.copy()
if quote_currency not in quote_currencies:
return None 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): def discovery(self):
connection = httplib.HTTPSConnection('intersango.com') connection = httplib.HTTPSConnection('intersango.com')
@ -26,12 +38,14 @@ class Exchanger:
# 2 = BTC:EUR # 2 = BTC:EUR
# 3 = BTC:USD # 3 = BTC:USD
# 4 = BTC:PLN # 4 = BTC:PLN
self.quote_currencies = {} quote_currencies = {}
try: try:
self.quote_currencies["GBP"] = self.lookup_rate(response, 1) quote_currencies["GBP"] = self.lookup_rate(response, 1)
self.quote_currencies["EUR"] = self.lookup_rate(response, 2) quote_currencies["EUR"] = self.lookup_rate(response, 2)
self.quote_currencies["USD"] = self.lookup_rate(response, 3) quote_currencies["USD"] = self.lookup_rate(response, 3)
self.refresh_balance() with self.lock:
self.quote_currencies = quote_currencies
self.parent.emit(SIGNAL("refresh_balance()"))
except KeyError: except KeyError:
pass pass

7
lib/gui_lite.py

@ -105,9 +105,10 @@ class MiniWindow(QDialog):
self.btc_balance = 0 self.btc_balance = 0
self.quote_currencies = ("EUR", "USD", "GBP") self.quote_currencies = ("EUR", "USD", "GBP")
self.exchanger = exchange_rate.Exchanger(self.quote_currencies, self.exchanger = exchange_rate.Exchanger(self)
self.refresh_balance) # Needed because price discovery is done in a different thread
QTimer.singleShot(1000, self.exchanger.discovery) # 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 = BalanceLabel(self.change_quote_currency)
self.balance_label.setObjectName("balance_label") self.balance_label.setObjectName("balance_label")

Loading…
Cancel
Save