@@ -352,24 +352,40 @@ def get_watchlist_prices():
|
||||
future = executor.submit(get_single_price, market, symbol)
|
||||
futures[future] = (market, symbol)
|
||||
|
||||
# 收集结果(保持顺序)
|
||||
for future in as_completed(futures, timeout=30):
|
||||
try:
|
||||
result = future.result()
|
||||
results.append(result)
|
||||
except Exception as e:
|
||||
market, symbol = futures[future]
|
||||
logger.error(f"Price fetch timed out or failed: {market}:{symbol} - {str(e)}")
|
||||
results.append({
|
||||
'market': market,
|
||||
'symbol': symbol,
|
||||
'price': 0,
|
||||
'change': 0,
|
||||
'changePercent': 0
|
||||
})
|
||||
# 收集结果(带超时保护)
|
||||
completed_futures = set()
|
||||
try:
|
||||
for future in as_completed(futures, timeout=30):
|
||||
completed_futures.add(future)
|
||||
try:
|
||||
result = future.result()
|
||||
results.append(result)
|
||||
except Exception as e:
|
||||
market, symbol = futures[future]
|
||||
logger.warning(f"Price fetch failed: {market}:{symbol} - {str(e)}")
|
||||
results.append({
|
||||
'market': market,
|
||||
'symbol': symbol,
|
||||
'price': 0,
|
||||
'change': 0,
|
||||
'changePercent': 0
|
||||
})
|
||||
except TimeoutError:
|
||||
# 超时时,为未完成的任务添加默认结果
|
||||
for future, (market, symbol) in futures.items():
|
||||
if future not in completed_futures:
|
||||
logger.warning(f"Price fetch timed out: {market}:{symbol}")
|
||||
results.append({
|
||||
'market': market,
|
||||
'symbol': symbol,
|
||||
'price': 0,
|
||||
'change': 0,
|
||||
'changePercent': 0,
|
||||
'error': 'timeout'
|
||||
})
|
||||
|
||||
success_count = sum(1 for r in results if r.get('price', 0) > 0)
|
||||
# logger.info(f"批量获取完成,成功: {success_count}/{len(results)}")
|
||||
logger.info(f"Watchlist prices: {success_count}/{len(results)} successful")
|
||||
|
||||
return jsonify({
|
||||
'code': 1,
|
||||
|
||||
Reference in New Issue
Block a user