0

Say I have a problem. There are three algorithms to solve that problem, name A, B, and C. Say that A is the baseline algorithm.

I would like to limit the run time to 10 seconds, but always allow algorithm A to finish. It means:

  • If before 10 seconds, 1 or more algorithms finish: I take the best one (I can know which result is better).
  • If 10 seconds always passed, I will wait until algorithm A finishes. It means that algorithm A must finish because otherwise there is no result to use.

I am consulting the page: How to limit execution time of a function call? but I think it does not answer my question directly.

Is there any Python library that can support me to do the task?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
mommomonthewind
  • 4,390
  • 11
  • 46
  • 74

1 Answers1

0

I think the following approach should work:

  • Create a multiprocessing.queue
  • Start three processes using multiprocessing.process, one for each algorithm, passing each one the queue as parameter. The processes each do their job and then send their result on the queue and exit.

The main process then goes into a loop, trying to receive a message from the queue, with zero or tiny timeout. It can exit the loop when 10s have elapsed, or it has sufficient responses. It can then use multiprocessing.terminate to kill the "slow coaches".

All relevant documentation is here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432