我有一个循环和从另一个函数获取数据的函数。我测试了来自邮递员的api调用,该调用在750毫秒内返回了数据,但由于此调用很重要,因此需要对其进行优化。
我尝试了线程化,但是却无法减少时间(也许我没有正确实现线程)。请任何类型的解决方案表示赞赏。
我的主要功能是:-
class QuoteDetailsList(APIView):
permission_class = default_permissions
def get(self, request, format = None):
try:
try:
source = LOCAL_DATA_SOURCE
fiat = request.GET.get("fiat", False)
if source not in ALLOWED_SOURCES:
source = GLOBAL_DATA_SOURCE
except Exception as e:
log_exception(e)
source = LOCAL_DATA_SOURCE
fiat = False
# MarketDataInterface().getAllQuotes(source, fiat)
return Response(MarketDataInterface().getAllQuotes(source, fiat), status = status.HTTP_200_OK)
return Response(data=dict(msg="KAR raha hun bhai"))
except InvalidDataError as e:
return Response({'error' : str(e)}, status.HTTP_400_BAD_REQUEST)
except BaseCryptoException as e:
return Response({'error' : str(e)}, status.HTTP_503_SERVICE_UNAVAILABLE)
getAllQuotes():-
def getAllQuotes(self, source, isFiat = False):
result = []
thr = []
if not isFiat:
symbols = Symbol.objects.all()
else:
symbols = Symbol.objects.exclude(Q(baseCurrency__isCrypto=True) | Q(quoteCurrency__isCrypto=True))
for symbol in symbols:
try:
if not symbol.baseCurrency.isBaseCurrency:
continue
result.append(self.getQuote(symbol.symbolId, source, symbol))
except Exception as e:
print("error",e)
pass
return result
是否忽略下一个正在运行的函数的条件是getQuote
getQuote:-
def getQuote(self, symbolId, source, symbol = None):
try:
if symbol is None:
symbol = Symbol.get(symbolId=symbolId)
t = self.getTrades(symbolId, 1, symbol)
try:
t = t[0]
except IndexError:
t = marketData_pb2.Trade()
a = self.getDepth(symbolId, 1, symbol)
try:
ask = a.sellDepth[0].price
except IndexError:
ask = 0
try:
bid = a.buyDepth[0].price
except:
bid = 0
fiat_price = 0
base_coin_price = 0
if symbol.baseCurrency.name == SYSTEM_BASE_COIN:
base_coin_price = t.price
else:
base_coin_price = getQuotePrice(symbol.quoteCurrency, LtpConversion().system_base_coin)
if symbol.baseCurrency.name == FIAT_COIN:
fiat_price = t.price
else:
fiat_price = getQuotePrice(symbol.quoteCurrency, LtpConversion().fiat_coin)
change = round_precision(CandleInterface().getChange(symbolId, t.price),2)
if FAKE_CHANGE:
if int(change*100) not in range(*CHANGE_RANGE):
change = random.randint(*CHANGE_RANGE)/100
vol = Trade.getVolume(symbol)
quoteDict = {
'symbol' : symbol.name,
'symbolId' : symbol.symbolId,
'lastTradedPrice' : t.price,
'lastTradedVolume': t.qty,
'askPrice' : ask,
'bidPrice' : bid,
'change' : change,
'volume' : round_precision(vol*base_coin_price, LtpConversion().system_base_coin.precision),
'volumeFiat' : round_precision(vol*fiat_price, LtpConversion().fiat_coin.precision),
'baseCurrency' : symbol.baseCurrency.name,
FIAT_COIN : fiat_price,
SYSTEM_BASE_COIN : base_coin_price
}
return quoteDict
except Exception as e:
log_exception(e)
quoteDict = {
'symbol' : symbol.name,
'symbolId' : symbol.symbolId,
'lastTradedPrice' : 0,
'lastTradedVolume': 0,
'askPrice' : 0,
'bidPrice' : 0,
'change' : 0,
'volume' : 0,
'baseCurrency' : symbol.baseCurrency.name,
}
return quoteDict
下一步功能通过redis调用进行了优化。因此,我认为这没有问题。虽然我可以更新任何人是否想要的问题。
我需要优化此API调用。 请帮忙。