1
0

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

This commit is contained in:
Amir Taaki
2012-07-02 22:39:45 +02:00
parent 9fd9f27408
commit bd489e998f
2 changed files with 30 additions and 15 deletions

View 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