Is there any solution in PYTHON?
Ian Bicking
ianb at colorstudy.com
Mon Jul 7 17:11:26 EDT 2003
On Mon, 2003-07-07 at 15:12, A wrote:
> Hi,
> I have a program that downloads some web pages. Sometimes there is a poor internet
> connection and my script freezes( hangs) and does not download ALL pages.
> Is there any solution how to test if the program works and if not re-start it from the point
> where it stopped?
> Or simply how to download ALL pages eventhough there is a timeout connection( can be
> of any value)?
Here ya' go:
class Timeout(Exception): pass
def timeout(time, func, *args):
def timeout_handler(signum, frame):
raise Timeout
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(time)
val = func(*args)
signal.alarm(0)
return val
To use:
def try_get_page(url):
# get the page, may block
def get_page(url):
while 1:
try:
page = timeout(10, try_get_page, url)
except Timeout:
pass
else:
return page
Note that this will not work in a multithreaded environment due to the
nature of the alarm signal.
Ian
More information about the Python-list
mailing list