Skip to content

Commit 2bfd66d

Browse files
committed
chore: use loop_lock for all run_until_complete usages
1 parent 8cad9c8 commit 2bfd66d

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

freqtrade/exchange/binance.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ def get_historic_ohlcv(
140140
:param candle_type: Any of the enum CandleType (must match trading mode!)
141141
"""
142142
if is_new_pair:
143-
x = self.loop.run_until_complete(
144-
self._async_get_candle_history(pair, timeframe, candle_type, 0)
145-
)
143+
with self._loop_lock:
144+
x = self.loop.run_until_complete(
145+
self._async_get_candle_history(pair, timeframe, candle_type, 0)
146+
)
146147
if x and x[3] and x[3][0] and x[3][0][0] > since_ms:
147148
# Set starting date to first available candle.
148149
since_ms = x[3][0][0]
@@ -201,16 +202,17 @@ def get_historic_ohlcv_fast(
201202
"""
202203
Fastly fetch OHLCV data by leveraging https://data.binance.vision.
203204
"""
204-
df = self.loop.run_until_complete(
205-
download_archive_ohlcv(
206-
candle_type=candle_type,
207-
pair=pair,
208-
timeframe=timeframe,
209-
since_ms=since_ms,
210-
until_ms=until_ms,
211-
markets=self.markets,
205+
with self._loop_lock:
206+
df = self.loop.run_until_complete(
207+
download_archive_ohlcv(
208+
candle_type=candle_type,
209+
pair=pair,
210+
timeframe=timeframe,
211+
since_ms=since_ms,
212+
until_ms=until_ms,
213+
markets=self.markets,
214+
)
212215
)
213-
)
214216

215217
# download the remaining data from rest API
216218
if df.empty:

freqtrade/exchange/exchange.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,8 @@ async def _api_reload_markets(self, reload: bool = False) -> dict[str, Any]:
648648

649649
def _load_async_markets(self, reload: bool = False) -> dict[str, Any]:
650650
try:
651-
markets = self.loop.run_until_complete(self._api_reload_markets(reload=reload))
651+
with self._loop_lock:
652+
markets = self.loop.run_until_complete(self._api_reload_markets(reload=reload))
652653

653654
if isinstance(markets, Exception):
654655
raise markets
@@ -2342,15 +2343,16 @@ def get_historic_ohlcv(
23422343
:param until_ms: Timestamp in milliseconds to get history up to
23432344
:return: Dataframe with candle (OHLCV) data
23442345
"""
2345-
pair, _, _, data, _ = self.loop.run_until_complete(
2346-
self._async_get_historic_ohlcv(
2347-
pair=pair,
2348-
timeframe=timeframe,
2349-
since_ms=since_ms,
2350-
until_ms=until_ms,
2351-
candle_type=candle_type,
2346+
with self._loop_lock:
2347+
pair, _, _, data, _ = self.loop.run_until_complete(
2348+
self._async_get_historic_ohlcv(
2349+
pair=pair,
2350+
timeframe=timeframe,
2351+
since_ms=since_ms,
2352+
until_ms=until_ms,
2353+
candle_type=candle_type,
2354+
)
23522355
)
2353-
)
23542356
logger.debug(f"Downloaded data for {pair} from ccxt with length {len(data)}.")
23552357
return ohlcv_to_dataframe(data, timeframe, pair, fill_missing=False, drop_incomplete=True)
23562358

0 commit comments

Comments
 (0)