import pathlib import threading import weakref import disk_paths import disk_utils DICTIONARY_LOCK = threading.Lock() DISK_LOCKS = weakref.WeakValueDictionary() def create_disk(request): paths = disk_paths.DiskPaths(request) workstation = request['workstation'] with disk_lock(paths): if not paths.target_path.exists(): disk_utils.create_os_disk(paths) if workstation['cpu'] == 'ARM' and not paths.nvram_target_path.exists(): disk_utils.create_nvram_disk(paths) def delete_disk(request): paths = disk_paths.DiskPaths(request) with disk_lock(paths): paths.target_path.unlink(missing_ok=True) paths.nvram_target_path.unlink(missing_ok=True) def disk_lock(paths): with DICTIONARY_LOCK: return DISK_LOCKS.setdefault(paths.target_filename, threading.Lock())