- if not do_refresh:
- # try reading from the cache first
- try: cfg = with_closing(file(cache_path))(lambda f: json.read(f.read()))
- except: do_refresh = True
+ if not ns.do_refresh:
+ # Try reading from the cache first. This must be transactionally
+ # isolated from concurrent writes to prevent reading an incomplete
+ # (changing) version of the data (but the transaction can share the
+ # lock with other concurrent reads).
+ @with_lock_file(lock_file, False)
+ def read_cache():
+ try: ns.cfg = with_closing(file(cache_path)) (
+ lambda f: json.read(f.read()))
+ except: ns.do_refresh = True