Skip to content

Commit db43f03

Browse files
authored
Merge pull request #18 from fractal-analytics-platform/17_retry_init
Add retry logic to ilastik initialisation, closes #17
2 parents 219491c + 453c30b commit db43f03

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/ilastik_tasks/ilastik_pixel_classification_segmentation.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import json
2424
import logging
2525
import os
26+
import time
2627
from typing import Any, Optional
2728

2829
import anndata as ad
@@ -176,6 +177,33 @@ def segment_ROI(
176177

177178
return ilastik_labels.astype(label_dtype)
178179

180+
def setup_ilastik_with_retries(ilastik_model: str):
181+
"""
182+
Setup Ilastik headless shell with retries to avoid initialization issues.
183+
184+
See #17 for context
185+
186+
"""
187+
max_retries = 5
188+
current_round = 0
189+
while current_round < max_retries:
190+
try:
191+
shell = setup_ilastik(ilastik_model)
192+
return shell
193+
except FileNotFoundError:
194+
current_round += 1
195+
logger.warning(
196+
f"Ilastik initialization failed, retrying {current_round=}/"
197+
f"{max_retries}"
198+
)
199+
sleep_time = 2 ** (current_round + 1)
200+
time.sleep(sleep_time)
201+
202+
raise FileNotFoundError(
203+
f"Ilastik initialization failed for model {ilastik_model} after "
204+
f"{max_retries}retries."
205+
)
206+
179207

180208
@validate_call
181209
def ilastik_pixel_classification_segmentation(
@@ -254,7 +282,7 @@ def ilastik_pixel_classification_segmentation(
254282
)
255283

256284
# Setup Ilastik headless shell
257-
shell = setup_ilastik(ilastik_model)
285+
shell = setup_ilastik_with_retries(ilastik_model)
258286

259287
# Check if channel input fits expected number of channels of model
260288
expected_num_channels = get_expected_number_of_channels(shell)

0 commit comments

Comments
 (0)