mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-08-05 16:58:59 +00:00
Adding a timelimit to fetching the feed.
This commit is contained in:
parent
27209c3164
commit
0805c7fc04
1 changed files with 31 additions and 0 deletions
|
@ -33,6 +33,36 @@ def mtime(ttime):
|
||||||
"""
|
"""
|
||||||
return datetime.datetime.fromtimestamp(time.mktime(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:
|
class FetchFeed:
|
||||||
def __init__(self, feed, options):
|
def __init__(self, feed, options):
|
||||||
|
@ -40,6 +70,7 @@ class FetchFeed:
|
||||||
self.options = options
|
self.options = options
|
||||||
self.fpf = None
|
self.fpf = None
|
||||||
|
|
||||||
|
@timelimit(20)
|
||||||
def fetch(self):
|
def fetch(self):
|
||||||
""" Downloads and parses a feed.
|
""" Downloads and parses a feed.
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Reference in a new issue