LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   python exception like structure with a timer (https://www.linuxquestions.org/questions/programming-9/python-exception-like-structure-with-a-timer-354577/)

shanenin 08-18-2005 11:35 AM

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


davholla 08-18-2005 11:39 AM

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.

Crashed_Again 08-18-2005 12:49 PM

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.

Hko 08-18-2005 01:24 PM

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"


shanenin 08-20-2005 10:44 AM

That last code worked, thanks. :-)


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