Adding a timelimit to fetching the feed.

This commit is contained in:
Samuel Clay 2010-08-31 20:17:27 -04:00
parent 27209c3164
commit 0805c7fc04

View file

@ -33,6 +33,36 @@ def mtime(ttime):
"""
return datetime.datetime.fromtimestamp(time.mktime(ttime))
import threading
class TimeoutError(Exception): pass
def timelimit(timeout):
"""borrowed from web.py"""
def _1(function):
def _2(*args, **kw):
class Dispatch(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = None
self.error = None
self.setDaemon(True)
self.start()
def run(self):
try:
self.result = function(*args, **kw)
except:
self.error = sys.exc_info()
c = Dispatch()
c.join(timeout)
if c.isAlive():
raise TimeoutError, 'took too long'
if c.error:
raise c.error[0], c.error[1]
return c.result
return _2
return _1
class FetchFeed:
def __init__(self, feed, options):
@ -40,6 +70,7 @@ class FetchFeed:
self.options = options
self.fpf = None
@timelimit(20)
def fetch(self):
""" Downloads and parses a feed.
"""