|
2 | 2 |
|
3 | 3 | import multiprocessing
|
4 | 4 | import threading
|
| 5 | +import uuid |
5 | 6 | import weakref
|
6 |
| -from collections.abc import MutableMapping |
7 |
| -from typing import Any |
8 |
| - |
9 |
| -try: |
10 |
| - from dask.utils import SerializableLock |
11 |
| -except ImportError: |
12 |
| - # no need to worry about serializing the lock |
13 |
| - SerializableLock = threading.Lock # type: ignore |
| 7 | +from collections.abc import Hashable, MutableMapping |
| 8 | +from typing import Any, ClassVar |
| 9 | +from weakref import WeakValueDictionary |
| 10 | + |
| 11 | + |
| 12 | +# SerializableLock is adapted from Dask: |
| 13 | +# https://github.com/dask/dask/blob/74e898f0ec712e8317ba86cc3b9d18b6b9922be0/dask/utils.py#L1160-L1224 |
| 14 | +# Used under the terms of Dask's license, see licenses/DASK_LICENSE. |
| 15 | +class SerializableLock: |
| 16 | + """A Serializable per-process Lock |
| 17 | +
|
| 18 | + This wraps a normal ``threading.Lock`` object and satisfies the same |
| 19 | + interface. However, this lock can also be serialized and sent to different |
| 20 | + processes. It will not block concurrent operations between processes (for |
| 21 | + this you should look at ``dask.multiprocessing.Lock`` or ``locket.lock_file`` |
| 22 | + but will consistently deserialize into the same lock. |
| 23 | +
|
| 24 | + So if we make a lock in one process:: |
| 25 | +
|
| 26 | + lock = SerializableLock() |
| 27 | +
|
| 28 | + And then send it over to another process multiple times:: |
| 29 | +
|
| 30 | + bytes = pickle.dumps(lock) |
| 31 | + a = pickle.loads(bytes) |
| 32 | + b = pickle.loads(bytes) |
| 33 | +
|
| 34 | + Then the deserialized objects will operate as though they were the same |
| 35 | + lock, and collide as appropriate. |
| 36 | +
|
| 37 | + This is useful for consistently protecting resources on a per-process |
| 38 | + level. |
| 39 | +
|
| 40 | + The creation of locks is itself not threadsafe. |
| 41 | + """ |
| 42 | + |
| 43 | + _locks: ClassVar[ |
| 44 | + WeakValueDictionary[Hashable, threading.Lock] |
| 45 | + ] = WeakValueDictionary() |
| 46 | + token: Hashable |
| 47 | + lock: threading.Lock |
| 48 | + |
| 49 | + def __init__(self, token: Hashable | None = None): |
| 50 | + self.token = token or str(uuid.uuid4()) |
| 51 | + if self.token in SerializableLock._locks: |
| 52 | + self.lock = SerializableLock._locks[self.token] |
| 53 | + else: |
| 54 | + self.lock = threading.Lock() |
| 55 | + SerializableLock._locks[self.token] = self.lock |
| 56 | + |
| 57 | + def acquire(self, *args, **kwargs): |
| 58 | + return self.lock.acquire(*args, **kwargs) |
| 59 | + |
| 60 | + def release(self, *args, **kwargs): |
| 61 | + return self.lock.release(*args, **kwargs) |
| 62 | + |
| 63 | + def __enter__(self): |
| 64 | + self.lock.__enter__() |
| 65 | + |
| 66 | + def __exit__(self, *args): |
| 67 | + self.lock.__exit__(*args) |
| 68 | + |
| 69 | + def locked(self): |
| 70 | + return self.lock.locked() |
| 71 | + |
| 72 | + def __getstate__(self): |
| 73 | + return self.token |
| 74 | + |
| 75 | + def __setstate__(self, token): |
| 76 | + self.__init__(token) |
| 77 | + |
| 78 | + def __str__(self): |
| 79 | + return f"<{self.__class__.__name__}: {self.token}>" |
| 80 | + |
| 81 | + __repr__ = __str__ |
14 | 82 |
|
15 | 83 |
|
16 | 84 | # Locks used by multiple backends.
|
|
0 commit comments