Skip to content

Commit 9d2f840

Browse files
author
Stefan Hahmann
committed
Add a retry mechanism for Python runtime exceptions in RegionPropsComputation
Introduce `runWithRetries` method to handle `PythonRuntimeException` with a retry logic, improving the robustness of the computation workflow.
1 parent 4f7d519 commit 9d2f840

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/main/java/org/mastodon/mamut/linking/trackastra/appose/computation/RegionPropsComputation.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.apposed.appose.NDArray;
3131
import org.apposed.appose.Service;
32+
import org.mastodon.mamut.detection.PythonRuntimeException;
3233
import org.mastodon.mamut.io.exporter.labelimage.ExportLabelImageUtils;
3334
import org.mastodon.mamut.linking.trackastra.TrackastraUtils;
3435
import org.mastodon.mamut.linking.trackastra.appose.types.SingleTimepointRegionProps;
@@ -131,7 +132,7 @@ private SingleTimepointRegionProps computeSingleTimepointProps( final Source< ?
131132
inputs.put( IMAGE, imageNDArray );
132133
inputs.put( MASK, masksNDArray );
133134

134-
Service.Task result = runScript();
135+
Service.Task result = runWithRetries( 1, 10 );
135136
ShmImg< IntType > labels = new ShmImg<>( ( NDArray ) result.outputs.get( LABELS ) );
136137
ShmImg< IntType > timepoints = new ShmImg<>( ( NDArray ) result.outputs.get( TIMEPOINTS ) );
137138
LoopBuilder.setImages( timepoints ).multiThreaded().forEachPixel( p -> p.set( timepoint ) ); // all timepoints are the same
@@ -148,6 +149,27 @@ private SingleTimepointRegionProps computeSingleTimepointProps( final Source< ?
148149
}
149150
}
150151

152+
private Service.Task runWithRetries( final int attempt, final int maxRetries ) throws IOException
153+
{
154+
try
155+
{
156+
return runScript();
157+
}
158+
catch ( PythonRuntimeException e )
159+
{
160+
if ( attempt <= maxRetries )
161+
{
162+
log.warn( "Python runtime exception on attempt {}/{}. Retrying...", attempt, maxRetries );
163+
return runWithRetries( attempt + 1, maxRetries );
164+
}
165+
else
166+
{
167+
log.error( "Python runtime exception on final attempt {}/{}. No more retries.", attempt, maxRetries );
168+
throw e;
169+
}
170+
}
171+
}
172+
151173
private void formatProgress( final int done, final int todo )
152174
{
153175
double progress = ( double ) done / todo;

0 commit comments

Comments
 (0)