[Tutor] setting up a timer

John Pertalion john@pertalion.org
Tue, 2 Jul 2002 17:51:22 -0400


Hello,

I've used timeoutsocket.py module when connecting to a MySQL database over a
questionable network (the Internet).  It lets you define a timeout in
seconds for ALL socket connections.  It throws a timeoutsocket.Timeout
exception when the connection isn't made within the timeout.  And has worked
great for me with connections using MySQLdb and smtplib.  Available at
http://www.timo-tasi.org/python/timeoutsocket.py

John Pertalion

> -----Original Message-----
> From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
> Lloyd Kvam
> Sent: Tuesday, July 02, 2002 2:28 PM
> To: Tutor Python
> Subject: [Tutor] setting up a timer
>
>
> I have some logic for starting and connecting to a MySQL database that I
> believe could get stuck because of network problems or remote server
> problems.  I'd like to have a timer to simply cancel the process if it is
> not successful in a reasonable time frame.  My search didn't turn up any
> examples, so I coded the following (my unix/C roots are showing).
>
> Then I discovered that signal.alarm is NOT available in Windows.  Mark
> Hammond's book (Python Programming on Win32) was no help.  Please point
> me in the right direction.
>
> def timeout( signo, frame):
> 	raise RuntimeError( "Timed out trying to connect to
> database server")
>
> def startup( login_name, passwd, svc_host, datadir, dbname):
> 	signal.signal( signal.SIGALRM, timeout)
> 	signal.alarm( 20)	# allow twenty seconds to succeed
> 	while 1:
> 		if did_startup_successfully():
> 			break
> 	signal.alarm(0)		# cancel alarm
>
> --
> Lloyd Kvam
> Venix Corp.
> 1 Court Street, Suite 378
> Lebanon, NH 03766-1358
>
> voice:
> 603-443-6155
> fax:
> 801-459-9582
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>