[Tutor] Re: setting up a timer
Lloyd Kvam
lkvam@venix.com
Wed, 03 Jul 2002 17:45:10 -0400
Thanks for the analysis. I have given up on a watchdog timer.
The timer need was coming from also starting the remote
MySQL server when necessary. I have lowered the requirements, so
that it will only start a local server.
Derrick 'dman' Hudson wrote:
> On Tue, Jul 02, 2002 at 07:47:02PM -0400, Lloyd Kvam wrote:
>
> First, it's too bad you can't use the standard, tried-and-true
> signal.alarm(). It would do exactly what you need.
>
> | Is there a way to kill a thread?
>
> Not if it is blocked. The only way to "kill" a thread is to terminate
> it's run() function from the inside.
>
> | Or is there some documentation about using exceptions with threads?
>
> If the exception occurs within the running thread, then the run()
> method should be able to catch it and terminate gracefully. (if it
> doesn't catch it then I think the thread will just die less
> gracefully)
>
> Each thread's run() method is like a separate main() (in a C program).
>
>
> Hmm, maybe something like this could work :
>
>
> import threading
>
> def foo() :
> raise Exception
>
> try :
> timer = threading.Timer( 10 , foo )
> timer.start()
> mysql.connect() # whatever
> timer.cancel() # we don't really want the exception anymore
> except Exception :
> print "timeout!"
>
>
> I just sorta tested it, and it seems that the Timer runs in a separate
> thread, so it has no effect on this thread.
>
> If the mysql connection is blocking and has no built-in timeout, I'm
> not sure if you can actually do anything about it. It's much easier
> to turn an async call into a sync one than the other way around
> (unless you have signal.alarm()!).
>
> HTH,
> -D
>
>
--
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice:
603-443-6155
fax:
801-459-9582