0

I have a GUI in which a python thread performs a scipy.curve_fit call. This can sometimes take a while and I would like to limit it to a certain time limit. If it takes longer than this I would like the thread to raise and error or return a default value.

I have searched around for different ways to do this. I can't make the thread regularly check for a timeout variable as when it starts running the curve_fit call it won't do anything else till it is finished.

Obviously this problem is not restricted to the curve_fit function but any library function that could take a long time to return, which I put in a thread.

Is this the best way to kill the thread?: Is there any way to kill a Thread in Python?

Is there a better more Pythonic way of doing this that I am missing?

Thank you for your help.

Community
  • 1
  • 1
user2175850
  • 193
  • 2
  • 13
  • Specifically on curve fitting, some of the functions can be passed a maximum number of function evaluations, which will limit the time used by the fitting routing. One example is leastsq() can be passed maxfev. – Jon Custer Jun 22 '15 at 15:44
  • Thanks, I had seen this parameter but haven't yet tried using it. I think this might be a bit tricky as depending on which function I am fitting and how many parameters I have the value for maxfev would be different for different calls. I think it would still be nicer if I had a way of timing out the thread. – user2175850 Jun 23 '15 at 12:04
  • On the other hand, you are asking for a calculation to be done, and are apparently trying to give the user an out if it is 'taking too long' - to me this means that the absolute value of maxfev isn't very important, just that the call will return within ~10 seconds on an 'average' machine. – Jon Custer Jun 23 '15 at 13:02

0 Answers0