|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
TimeOut inHi 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@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: TimeOut in"somnath chakrabarti" <chakrabarti.somnath@...> wrote > 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? I have no idea how this framework is upposed to work but my guess is that the problem lies here: > def getSolution(self,solver): > Problem.setSolver(self,solver) > self.p = threading.Timer(100, self.onTimeout()) Thios looks like you should be providing a callback function self.onTimeout but you are calling the function instead of referencing it. This calls the method before self.p has been defined. You need to remove the parentheses after Timeout. I think... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: TimeOut inHi Alan,
I am importing the constraint library package in the beginning that is having classes Problem, Solver and all the methods that I am calling. Now, as you said after making the change from
self.p = threading.Timer(100, self.onTimeout)
I am getting the below error. I am also attaching the constraint.py package that my code is using
- Somnath
%%%%%%% Error Message %%%%%%%%%%%%%%%%%%%%
<constraint.BacktrackingSolver object at 0x02846F10>
Magic Square: 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 43, in getSolution self.solution=Problem.getSolution(self) File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line 215, in getSolution return self._solver.getSolution(domains, constraints, vconstraints) File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line 524, in getSolution return iter.next() File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line 506, in getSolutionIter pushdomains): File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line 1173, in __call__ domain.hideValue(value) File "C:\Users\Somnath\Documents\NetBeansProjects\CSC671\src\constraint.py", line 787, in hideValue list.remove(self, value) ValueError: list.remove(x): x not in list None Time taken: 0.047 Traceback (most recent call last): On Sun, Nov 8, 2009 at 12:17 PM, Alan Gauld <alan.gauld@...> wrote:
-- Thanks and regards, Somnath Chakrabarti. _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: TimeOut inHi Somnath,
I think the exception simply means that the thread object or class does not have a cancel function. maybe read docs on Python threads to see what functions can be called on those?
On Sun, Nov 8, 2009 at 9:09 PM, somnath chakrabarti <chakrabarti.somnath@...> wrote:
-- Regards, Lloyd _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
| Free embeddable forum powered by Nabble | Forum Help |