0

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)

Slyphlamen
  • 26
  • 6
  • I don't know if that could help, but you might find a way with the main reply on this subject: https://stackoverflow.com/questions/366682/how-to-limit-execution-time-of-a-function-call?newreg=ffc80c5b5be34984b61455d6f8c316b0 – Verdier Francois May 06 '22 at 11:54
  • Indeed, it's a bit hidden but the contribution of erickfis was exactly what I was looking for with the func-timeout package ! I guess it makes this thread a copy of the other one, but his solution should be put more forward as it works on Windows. – Slyphlamen May 06 '22 at 12:07

0 Answers0