Do slow price discovery in another thread to speed up startup time and responsiveness.
This commit is contained in:
@@ -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):
|
||||||
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
|
return None
|
||||||
if quote_currency not in self.quote_currencies:
|
return btc_amount * quote_currencies[quote_currency]
|
||||||
return None
|
|
||||||
return btc_amount * self.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
|
||||||
|
|
||||||
|
|||||||
@@ -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")
|
||||||
|
|||||||
Reference in New Issue
Block a user