MCPcopy Create free account
hub / github.com/dask/dask / SerializableLock

Class SerializableLock

dask/utils.py:1285–1349  ·  view source on GitHub ↗

A Serializable per-process Lock This wraps a normal ``threading.Lock`` object and satisfies the same interface. However, this lock can also be serialized and sent to different processes. It will not block concurrent operations between processes (for this you should look at ``multi

Source from the content-addressed store, hash-verified

1283
1284
1285class SerializableLock:
1286 """A Serializable per-process Lock
1287
1288 This wraps a normal ``threading.Lock`` object and satisfies the same
1289 interface. However, this lock can also be serialized and sent to different
1290 processes. It will not block concurrent operations between processes (for
1291 this you should look at ``multiprocessing.Lock`` or ``locket.lock_file``
1292 but will consistently deserialize into the same lock.
1293
1294 So if we make a lock in one process::
1295
1296 lock = SerializableLock()
1297
1298 And then send it over to another process multiple times::
1299
1300 bytes = pickle.dumps(lock)
1301 a = pickle.loads(bytes)
1302 b = pickle.loads(bytes)
1303
1304 Then the deserialized objects will operate as though they were the same
1305 lock, and collide as appropriate.
1306
1307 This is useful for consistently protecting resources on a per-process
1308 level.
1309
1310 The creation of locks is itself not threadsafe.
1311 """
1312
1313 _locks: ClassVar[WeakValueDictionary[Hashable, Lock]] = WeakValueDictionary()
1314 token: Hashable
1315 lock: Lock
1316
1317 def __init__(self, token: Hashable | None = None):
1318 self.token = token or str(uuid.uuid4())
1319 if self.token in SerializableLock._locks:
1320 self.lock = SerializableLock._locks[self.token]
1321 else:
1322 self.lock = Lock()
1323 SerializableLock._locks[self.token] = self.lock
1324
1325 def acquire(self, *args, **kwargs):
1326 return self.lock.acquire(*args, **kwargs)
1327
1328 def release(self, *args, **kwargs):
1329 return self.lock.release(*args, **kwargs)
1330
1331 def __enter__(self):
1332 self.lock.__enter__()
1333
1334 def __exit__(self, *args):
1335 self.lock.__exit__(*args)
1336
1337 def locked(self):
1338 return self.lock.locked()
1339
1340 def __getstate__(self):
1341 return self.token
1342

Callers 7

test_SerializableLockFunction · 0.90
from_arrayFunction · 0.90
_layerMethod · 0.90
get_scheduler_lockFunction · 0.85

Calls

no outgoing calls