Created
May 1, 2013 14:53
-
-
Save alexstorer/5495736 to your computer and use it in GitHub Desktop.
Using signal in python for molly!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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