Skip to content

Commit

Permalink
Lock writing the cached file, this avoids requesting the same file ma…
Browse files Browse the repository at this point in the history
…ny times
  • Loading branch information
alexamici committed Mar 29, 2024
1 parent 8a85756 commit b05044d
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions xarray_ecmwf/engine_ecmwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hashlib
import logging
import os
import socket
import uuid
from typing import Any, Callable, Iterable, Iterator, Sequence

Expand All @@ -13,6 +14,7 @@
from . import client_cdsapi, client_common, client_ecmwf_opendata, client_polytope

LOGGER = logging.getLogger(__name__)
HOSTNAME = socket.gethostname()

SUPPORTED_CLIENTS: dict[str, type[client_common.RequestClientProtocol]] = {
"cdsapi": client_cdsapi.CdsapiRequestClient,
Expand Down Expand Up @@ -89,8 +91,9 @@ def retrieve(
if not os.path.isdir(self.cache_folder):
os.makedirs(self.cache_folder, exist_ok=True)

if not os.path.exists(path):
robust_save_to_file(self.request_client.download, (result,), path)
with xr.backends.locks.get_write_lock(f"{HOSTNAME}/{filename}"): # type: ignore
if not os.path.exists(path):
robust_save_to_file(self.request_client.download, (result,), path)
ds = self.open_dataset(path)
LOGGER.debug("request: %r ->\n%r", request, list(ds.data_vars.values())[0])
try:
Expand Down

0 comments on commit b05044d

Please sign in to comment.