Trying to do a write operation to multiple IDS in multiple threads e.g
setattr(core_profiles_ids.profiles_1d.ion[0], 'name', 'D')
results in
AttributeError: type object 'IDSMetadata' has no attribute '__setattr__'. Did you mean: '__delattr__'?
Using imas-core. I believe this is because get_toplevel_metadata() temporarily deletes
IDSMetadata.__setattr__
I was able to avoid the error by adding a lock to get_toplevel_metadata
import threading
from imas import ids_metadata as _ids_metadata
from imas import ids_toplevel as _ids_toplevel
def _patch_imas_metadata_lock() -> None:
"""Patches IMAS metadata construction to be thread-safe.
The upstream ``get_toplevel_metadata()`` temporarily *deletes*
``IDSMetadata.__setattr__`` from the class to allow construction,
then restores it. This is not thread-safe.
"""
_imas_metadata_lock = threading.Lock()
_orig_get_toplevel_metadata = _ids_metadata.get_toplevel_metadata
def _thread_safe_get_toplevel_metadata(structure_xml):
with _imas_metadata_lock:
return _orig_get_toplevel_metadata(structure_xml)
_ids_metadata.get_toplevel_metadata = _thread_safe_get_toplevel_metadata
# Also patch the reference that ids_toplevel already imported.
_ids_toplevel.get_toplevel_metadata = _thread_safe_get_toplevel_metadata
Trying to do a write operation to multiple IDS in multiple threads e.g
results in
Using imas-core. I believe this is because
get_toplevel_metadata()temporarily deletesIDSMetadata.__setattr__I was able to avoid the error by adding a lock to
get_toplevel_metadata