Hi Python Tutor members, I have a requirement where I am trying to give a solution for Magic Square puzzle for sizes ranging from 3 to 5 and using different Solvers. I have the below MS class derived from Problem class which is defined in constraint.py. The Problem class has a method called getSolution() which I am overriding in derived class MS. In MS.getSolution(solver), I am trying to start a threading.Timer object that will execute MS.onTimeout() method when time exceeds 100 secs i.e. if there is delay more than 100 secs for the Problem.getSolution() method, the timer thread will execute onTimeout and cancel the timer thread. Once the timer thread is canceled, the control must return back to the magic_square(n,solver=BacktrackingSolver()) function at the control point ms.p.join() and it would print the time taken. Finally, it will close the thread in magic_square.
Below is the code that I have written to do the timeout implementation. But I am getting error as follows. Can anybody please help me where I am going wrong? %%%%%%Error Message %%%%%%%%%%%%%%%%%%%%%%%%%%% Traceback (most recent call last): File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\csc671.py", line 125, in <module> <constraint.BacktrackingSolver object at 0x02845F50> ms_t.magic_square(size,func_solver()) Magic Square: None File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py", line 65, in magic_square print ms.getSolution(solver) Time Exceeded Limit.None Time Exceeded Limit. File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py", line 39, in getSolution self.p = threading.Timer(100, self.onTimeout()) File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py", line 52, in onTimeout if self.p.isAlive(): AttributeError: 'MS' object has no attribute 'p' Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python26\lib\threading.py", line 525, in __bootstrap_inner self.run() File "C:\Python26\lib\threading.py", line 477, in run self.__target(*self.__args, **self.__kwargs) File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py", line 39, in getSolution self.p = threading.Timer(100, self.onTimeout()) File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\ms_t.py", line 52, in onTimeout if self.p.isAlive(): AttributeError: 'MS' object has no attribute 'p' %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Below is the code --> from __future__ import generators from constraint import * import time #import thread import threading #import sys #import signal global t class MS(Problem): start = 0 end = 0 def __init__(self,solver=None,size=3): Problem.__init__(self,solver) Problem.addVariables(self,range(0,size*size),range(1,size*size+1)) self.addConstraints(size) self.solution = {} def addConstraints(self,size): magic_sum = (size*(size*size + 1))/2 Problem.addConstraint(self,AllDifferentConstraint(),range(0,size*size)) Problem.addConstraint(self,ExactSumConstraint(magic_sum),[i for i in range(0,size*size,size+1)]) Problem.addConstraint(self,ExactSumConstraint(magic_sum),[i for i in range(size-1,size*(size-1)+1,size-1)]) for row in range(size): Problem.addConstraint(self,ExactSumConstraint(magic_sum),[row*size+i for i in range(size)]) for col in range(size): Problem.addConstraint(self,ExactSumConstraint(magic_sum),[col+size*i for i in range(size)]) def getSolution(self,solver): Problem.setSolver(self,solver) self.p = threading.Timer(100, self.onTimeout()) self.start=time.time() self.p.start() try: self.solution=Problem.getSolution(self) finally: self.end=time.time() self.p.cancel() # dummy signal handler for timeout def onTimeout(self): print None print 'Time Exceeded Limit.' if self.p.isAlive(): self.p.cancel() def magic_square(n,solver=BacktrackingSolver()): ms=MS(solver,n) print solver print 'Magic Square:' t = threading.Thread(None,ms.getSolution,None,(solver,),{}) t.start() print ms.getSolution(solver) ms.p.join() print 'Time taken: %1.3f' % (ms.end-ms.start) t.cancel() -- Thanks and regards, Somnath Chakrabarti.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor