I am running simulations that take as input a parameter, and I loop over lots of parameters to try different results. Some simulations sometimes get stuck trying to converge to a solution forever. How do you make it so that if one simulation takes longer than let's say 10s, that simulation stops and the loop moves on to the next simulation ?
Schematically my situation would be like :
for param in parameters:
TIMEOUT = 10
start = time.time()
while (time.time()-start<TIMEOUT): #however while only checks the condition once and not continuously
my_simulation_script(param)
I figured that the best way might be to work with Threads of Processes, but I rarely worked with those and I'm having a hard time seeing how to make it work.
I tried following this topic Timeout on a function call but it does not work well since from what I understand it stills runs top to bottom so that p.join(10) is only called once it went through the simulation (and my issue is that it might get stuck in it trying to converge to a solution forever) + the fact that with this way you have to wait 10s for each simulation.
for param in parameters:
p=multiprocessing.Process(target=my_simulation_script(param))
p.start()
p.join(10)
if p.is_alive():
p.terminate()
I would imagine having two parallel Processes, one managing time, one managing the simulations in the loop, could work by having the time managing process take care of the TIMEOUT but I'm not sure how to go at it.
Edit : I was thinking maybe I could send a "continue" command kind of like in this post How to pass exception from one process to another? but no success in coding it properly so far
EDIT EDIT : I guess the question could be summed up to : how to make two processes "compete" ? (A and B processes, if A finishes first kill B instantaneously and likewise if B finishes first kill A instantaneously)