python - Maximum time to wait for url response -


i'm making simple update checker function code set run every time before bulk of code executed. notifies user new version available download.

here's mwe:

import urllib  def updater(__version__):     try:         # latest version number master repo @ github.         f = urllib.urlopen("https://raw.githubusercontent.com/chrisglass"             "/django_polymorphic/master/polymorphic/__version__.py")         s = f.read().split('"')          if s[-2] != __version__:             print "new version {} available.".format(s[-2])     except:         pass  # call function check if new version available. __version__ = '0.1' updater(__version__) 

(that repo isn't mine, i'm using in example because use similar version of __version.py__ file)

this works fine, i'm concerned github taking long respond, hold execution of code.

is there way jump out of try block after 5 seconds have elapsed? recommended way go this?

use urllib2, urlopen has timeout parameter.

urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])

https://docs.python.org/2/library/urllib2.html


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -