electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit bd489e998fe159ad662830ec1d60f8acec4e133d
parent 9fd9f27408209787f419ac4a1ecd268252b12382
Author: Amir Taaki <genjix@riseup.net>
Date:   Mon,  2 Jul 2012 22:39:45 +0200

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

Diffstat:
Mlib/exchange_rate.py | 38++++++++++++++++++++++++++------------
Mlib/gui_lite.py | 7++++---
2 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/lib/exchange_rate.py b/lib/exchange_rate.py @@ -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 diff --git a/lib/gui_lite.py b/lib/gui_lite.py @@ -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")