try:
html = urlopen('http://glbse.com/api/asset/' + asset.name)
except:
print 'error while updating the price of ' + asset.name
continue
json_txt = html.read()
ticker = json.loads(json_txt)
average_price = int(ticker['t24havg'])
if average_price == 0:
average_price = int(ticker['t5davg'])
if average_price == 0:
average_price = int(ticker['t7davg'])
if average_price == 0:
average_price = int(ticker['latest_trade'])
if average_price == 0:
print 'could not determine the price of ' + asset.name
continue
asset.average_price = average_price
I am using mechanize for urlopen.
This code (and the rest of the program) seems to run fine for hours but, after having looped through this section thousands of times, will eventually hang somewhere in this section of code.
It hangs for an indefinite length of time. I’ve even come back to it to find it had hung there for hours.
Googling the issue all I come up with are reports of a similar issue, where execution hangs on .read() which is reported to be fixed years ago.
So what is causing execution to hang and how can I fix or workaround it?
using
mechanize.Browser().open()instead ofurlopenreveals a “urllib2.URLError urlopen connection time out” which is not raised when usingurlopenalone. I strongly suspect this is the problem and my solution is to usemechanize.Browser().open()in place ofurlopenin all cases