LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-18-2005, 11:35 AM   #1
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
python exception like structure with a timer


I have a program that depends on being able to query a online database. If the internet connection fails, I want the program to continue, but with different parameters. I am currently using an exception like the following:
Code:
try: 
        (query_status, query_info) = CDDB.query(disc_id) 
        results = cd_info(query_status, query_info, disc_id)
        
except:
        do something here
this above method works, but unsatisfactory. The problem is I need to wait for over a minute, maybe two for it ito time out.

Is there a method I could use, to place my two lines of code, like an exception. The result would be if the commands did not finish executing in a set amount of time(say 20 seconds). a secondary clause would execute.

for example:
Code:
try:
        (query_status, query_info) = CDDB.query(disc_id) 
        results = cd_info(query_status, query_info, disc_id)
if not finished in 20 seconds:
        execute this clause

Last edited by shanenin; 08-18-2005 at 11:38 AM.
 
Old 08-18-2005, 11:39 AM   #2
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Mandriva 2008 Spring
Posts: 652

Rep: Reputation: 31
How about :-

import time
x = time.time()
#your code here
y = time.time()
if y > (x+20):
do a
else:
do b


I have not tested this but I think it should work. Let me know.
 
Old 08-18-2005, 12:49 PM   #3
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Quote:
Originally posted by davholla
How about :-

import time
x = time.time()
#your code here
y = time.time()
if y > (x+20):
do a
else:
do b


I have not tested this but I think it should work. Let me know.
Yeah even better is:

Code:
import time
time.sleep(60)
Which sleeps 60 seconds.
 
Old 08-18-2005, 01:24 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by davholla
How about :-

import time
x = time.time()
#your code here
y = time.time()
if y > (x+20):
do a
else:
do b
That will not do the trick: The call to CDDB.query() will take 1 or 2 minutes. Your code only measures the time it took afterwards.

I think the question was how to make the call to CDDB.query() take less time in case the connection fails.

@shanenin
I don't know about the CDDB-module, but did you look for some way in that module to specify the time-out? That would be the prefferred solution of course. If there's no such way, this might help:

Code:
#!/usr/bin/env python

import time
import signal

# Signal hander function.
# Makes an Exception occur.
def alarmHandler(signalNumber, stackFrame):
	raise IOError, "Time-out"

# Setting handler function.
# The handler will be run when a SIGALARM
# interrupts the program
signal.signal(signal.SIGALRM, alarmHandler)

# Setting the shorter time-out here: 5 secs.
# This will interrupt the program after 5 secs
# from now.
# Comment out the line below to simulate normal
# time-out or successful query.
signal.alarm(5)

try:
	# Simulate a query timing out in 1 minute.
	print "Start (simulated) query time-out"
	time.sleep(60)  # Put your query-call here instead.
except IOError:
	print "Time out! " * 5

print "Finished"

Last edited by Hko; 08-18-2005 at 01:49 PM.
 
Old 08-20-2005, 10:44 AM   #5
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Original Poster
Rep: Reputation: 30
That last code worked, thanks. :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Fedora 2 04-23-2009 07:02 PM
help createing exception class from base STL exception qwijibow Programming 4 04-20-2005 05:23 AM
eric3 QT python editor exception... cuboidz Linux - Software 0 02-08-2004 11:16 AM
python; exception handling TheLinuxDuck Programming 2 08-20-2003 02:01 PM
Runtime Exception vs. Exception mikeshn Programming 1 09-22-2002 05:33 AM


All times are GMT -5. The time now is 03:03 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration