Skip to content

Commit d7fa858

Browse files
committed
fix: file contention when multiple libero instances init
When multiple libero instances run for the first time, they all try to create the config directory and file, causing contention. This commit adds a file lock to prevent contention Signed-off-by: Hao Lin <[email protected]>
1 parent 811c049 commit d7fa858

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

libero/libero/__init__.py

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import yaml
3+
from filelock import FileLock
34

45
# This is a default path for localizing all the benchmark related files
56
libero_config_path = os.environ.get(
@@ -52,45 +53,25 @@ def get_libero_path(query_key):
5253

5354
def set_libero_default_path(custom_location=os.path.dirname(os.path.abspath(__file__))):
5455
print(
55-
f"[Warning] You are changing the default path for Libero config. This will affect all the paths in the config file."
56+
"[Warning] You are changing the default path for Libero config. This will affect all the paths in the config file."
5657
)
5758
new_config = get_default_path_dict(custom_location)
5859
with open(config_file, "w") as f:
5960
yaml.dump(new_config, f)
6061

61-
62-
if not os.path.exists(libero_config_path):
63-
os.makedirs(libero_config_path)
62+
with FileLock(".config_lock"):
63+
if not os.path.exists(libero_config_path):
64+
os.makedirs(libero_config_path)
6465

6566
if not os.path.exists(config_file):
6667
# Create a default config file
6768

6869
default_path_dict = get_default_path_dict()
69-
answer = input(
70-
"Do you want to specify a custom path for the dataset folder? (Y/N): "
71-
).lower()
72-
if answer == "y":
73-
# If the user wants to specify a custom storage path, prompt them to enter it
74-
custom_dataset_path = input(
75-
"Enter the path where you want to store the datasets: "
76-
)
77-
full_custom_dataset_path = os.path.join(
78-
os.path.abspath(os.path.expanduser(custom_dataset_path)), "datasets"
79-
)
80-
# Check if the custom storage path exists, and create if it doesn't
81-
82-
print("The full path of the custom storage path you entered is:")
83-
print(full_custom_dataset_path)
84-
print("Do you want to continue? (Y/N)")
85-
confirm_answer = input().lower()
86-
if confirm_answer == "y":
87-
if not os.path.exists(full_custom_dataset_path):
88-
os.makedirs(full_custom_dataset_path)
89-
default_path_dict["datasets"] = full_custom_dataset_path
9070
print("Initializing the default config file...")
9171
print(f"The following information is stored in the config file: {config_file}")
9272
# write all the paths into a yaml file
93-
with open(config_file, "w") as f:
94-
yaml.dump(default_path_dict, f)
95-
for key, value in default_path_dict.items():
96-
print(f"{key}: {value}")
73+
with FileLock(".config_lock"):
74+
with open(config_file, "w") as f:
75+
yaml.dump(default_path_dict, f)
76+
for key, value in default_path_dict.items():
77+
print(f"{key}: {value}")

0 commit comments

Comments
 (0)