|
16 | 16 | import sys |
17 | 17 | import subprocess |
18 | 18 | import keyword |
| 19 | +import threading |
19 | 20 | from inspect import getcallargs |
20 | 21 | from functools import wraps |
21 | 22 |
|
@@ -2187,8 +2188,10 @@ def generate_connection_table(hdf5_file): |
2187 | 2188 | # replace the outdated cache entry with a new list of updated vcs commands and |
2188 | 2189 | # outputs. |
2189 | 2190 | _vcs_cache = {} |
| 2191 | +_vcs_cache_rlock = threading.RLock() |
2190 | 2192 | def _file_watcher_callback(name, info, event): |
2191 | | - _vcs_cache[name] = _run_vcs_commands(name) |
| 2193 | + with _vcs_cache_rlock: |
| 2194 | + _vcs_cache[name] = _run_vcs_commands(name) |
2192 | 2195 |
|
2193 | 2196 | _file_watcher = FileWatcher(_file_watcher_callback) |
2194 | 2197 |
|
@@ -2283,17 +2286,15 @@ def save_labscripts(hdf5_file): |
2283 | 2286 | # Doesn't seem to want to double count files if you just import the contents of a file within a module |
2284 | 2287 | continue |
2285 | 2288 | hdf5_file.create_dataset(save_path, data=open(path).read()) |
2286 | | - if path not in _file_watcher.files: |
2287 | | - # Add file to watch list and create its entry in the cache. |
2288 | | - _file_watcher.add_file(path) |
2289 | | - _file_watcher_callback(path, None, None) |
2290 | | - # Get a reference to the current results list in case |
2291 | | - # another thread updates _vcs_cache[path] to point at a new |
2292 | | - # list during this loop. |
2293 | | - results = _vcs_cache[path] |
2294 | | - for command, info, err in results: |
2295 | | - attribute_str = command[0] + ' ' + command[1] |
2296 | | - hdf5_file[save_path].attrs[attribute_str] = (info + '\n' + err) |
| 2289 | + with _vcs_cache_rlock: |
| 2290 | + if path not in _vcs_cache: |
| 2291 | + # Add file to watch list and create its entry in the cache. |
| 2292 | + _file_watcher.add_file(path) |
| 2293 | + _file_watcher_callback(path, None, None) |
| 2294 | + # Save the cached vcs output to the file. |
| 2295 | + for command, info, err in _vcs_cache[path]: |
| 2296 | + attribute_str = command[0] + ' ' + command[1] |
| 2297 | + hdf5_file[save_path].attrs[attribute_str] = (info + '\n' + err) |
2297 | 2298 | except ImportError: |
2298 | 2299 | pass |
2299 | 2300 | except WindowsError if os.name == 'nt' else None: |
|
0 commit comments