Skip to content

Instantly share code, notes, and snippets.

@alexstorer
Created May 1, 2013 14:53
Show Gist options
  • Save alexstorer/5495736 to your computer and use it in GitHub Desktop.
Save alexstorer/5495736 to your computer and use it in GitHub Desktop.
Using signal in python for molly!
def handler(signum, frame):
raise Exception('Last Resort!')
signal.signal(signal.SIGALRM, handler)
def tryOpenParse(thisurl):
global parser
#print "trying to parse " + thisurl
print "trying to parse..."
loaded = False
counter = 0
tree = None
while ((not loaded) and (counter<5)):
if counter>0:
print "Failed so far: ", counter
print thisurl
if counter>10:
return None
try:
counter+=1
signal.alarm(5)
u = urllib2.urlopen(thisurl)
tree = etree.parse(u, parser)
signal.alarm(0)
loaded = True
except Exception,e:
print e
signal.alarm(0)
time.sleep(5*random.random())
print "...done!"
return tree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment