[Tutor] extract uri from beautiful soup string

Steven D'Aprano steve at pearwood.info
Sun Oct 14 21:42:09 CEST 2012


On 15/10/12 05:05, Norman Khine wrote:

> for page_no in pages:
[...]
> 	try:
> 		urllib2.urlopen(req)
> 	except urllib2.URLError, e:
> 		pass
> 	else:
> 		# do something with the page
> 		doc = urllib2.urlopen(req)

This is a bug. Just because urlopen(req) succeeded a moment ago, doesn't mean
that it will necessarily succeed now.

This is an example of the general class of bug known as a "race condition":

http://en.wikipedia.org/wiki/Race_condition


Better to write this as:


try:
     doc = urllib2.urlopen(req)
except urllib2.URLError, e:
     pass
else:
     # do something with the page
     [rest of code goes here]




-- 
Steven


More information about the Tutor mailing list