[Tutor] timeout a routine
arbaro arbaro
arbaro at gmail.com
Tue Nov 28 19:35:17 CET 2006
Thanks a lot Jordan,
I have no experience with classes at all yet, so your example will
give me a really nice startting point.
On 11/28/06, Jordan Greenberg <jordangreenberg at gmail.com> wrote:
> arbaro arbaro wrote:
> > Hello,
> <SNIP>
> > Is there a way to run the command
> > os.path.isdir("/mnt/server/folderOnServer"), and kill the attempt if
> > it tries for longer then 5 seconds?
> > Where/what can I use to accomplish something like this?
> >
> >
> > Thanks,
> > Arbaro
>
> Hi Arbaro!
> The only way I can think of is with threads.
> Its not too hard to use one thread to control another, and stop it after
> a certain amount of time. Something like this:
>
> #threadexample.py
>
> from threading import Thread
> import time
>
> MyThread(Thread):
> def __init__(self, message):
> self.message=message
> Thread.__init__(self)
> def run(self):
> while True:
> print "MyThread: "+self.message
>
> class Controller(Thread):
> def __init__(self, runner,time):
> self.runner=runner
> self.delta=time
> Thread.__init__(self)
>
> def run(self):
> self.start=time.time()
> self.runner.start()
> self.done=False
> while not self.done:
> if time.time() > (self.start+self.delta):
> print "Controller: Stopping MyThread."
> self.runner._Thread__stop()
> return
>
> mythread=MyThread("hello")
> control=Controller(mythread, 2)
> control.start()
>
> #end of threadexample.py
>
> I'm still just learning threading, so there is probably a better way to
> do this, though this seems to work pretty well.
>
>
> Hope this helps,
> Jordan Greenberg
>
More information about the Tutor
mailing list