dragon.utils
Functions
|
|
|
|
|
|
|
|
|
|
|
This is used to get a hostid based on the k8s pod uid. |
|
|
|
|
|
|
|
|
|
Convert a string representation of truth to true (1) or false (0). |
Classes
Cython wrapper for Dragon's byte <> string conversion routines. |
|
Enhanced threading.Thread that can be killed from the outside by raising an exception inside the running instance. |
|
Tracks elapsed times by id and optionally streams them to the Dragon telemetry infrastructure on a background thread. |
|
A Pickler that has X-language support and is compatible with the C++ SerializableByteBuffer. |
|
A Pickler that has X-language support and is compatible with the C++ Serializable2DMatrix<Type>. |
|
A Pickler that has X-language support and is compatible with the C++ SerializableVector<Type>. |
|
A Pickler that has X-language support and is compatible with the C++ Serializables. |
|
A Pickler that has X-language support and is compatible with the C++ Scalars like int and double. |
|
A Pickler that has X-language support and is compatible with the C++ SerializableString. |
- class B64
Bases:
objectCython wrapper for Dragon’s byte <> string conversion routines.
- __init__()
Convert a bytes array into a base64 encoded string. :param data: The list of bytes to convert. :return: A new B64String object containing the base64 encoded string.
- classmethod bytes_to_str(the_bytes)
Converts bytes into a string by base64 encoding it. Convenience function to convert bytes objects to base64 encoded strings. :param the_bytes: bytes to get encoded :return: string
- decode()
- classmethod from_str(serialized_str)
- classmethod str_to_bytes(the_str)
Converts a base64 encoded string to a bytes object. Convenience function to unpack strings. :param the_str: base64 encoded string. :return: original bytes representation.
- class ExceptionalThread
Bases:
ThreadEnhanced threading.Thread that can be killed from the outside by raising an exception inside the running instance. The thread’s running target function must reach a point where the exception can be noticed such as when the GIL swaps executing threads or some other wait state occurs. Note that external native library (C/C++/Fortran) code invoked within the target function will not notice the exception being raised and so control must be returned to Python for the exception to be noticed.
>>> import time >>> t1 = ExceptionalThread( ... target=lambda n: sum(1 if y % 2 == 0 else -1 for y in range(n)), ... args=(10_000_000_000,) ... ) # Compute should take several minutes on modern processor core >>> start_time = time.monotonic(); t1.start() >>> t1.kill_by_exception() 1 >>> t1.join() >>> (time.monotonic() - start_time) < 1 # Killed and joined in under 1s True >>> # time.sleep ignores exception until very end like native code would >>> t2 = ExceptionalThread(target=time.sleep, args=(1,)) >>> start_time = time.monotonic(); t2.start() >>> t2.kill_by_exception() # Still indicates exception trigger success 1 >>> t2.join() >>> (time.monotonic() - start_time) < 1 # No early termination of thread False
- kill_by_exception(exc_type=<class 'Exception'>)
When called, will raise the specified exception inside the running thread instance and will return an int to indicate success (1), failure to find the thread (0), or unhandled error followed by an attempt to revert the thread state change (2 or greater).
- class SerType
Bases:
IntFlag- TY_NONE = 0
- TY_STR = 1
- TY_INT = 2
- TY_DOUBLE = 3
- TY_INTVECTOR = 4
- TY_DOUBLEVECTOR = 5
- TY_INTMATRIX = 6
- TY_DOUBLEMATRIX = 7
- TY_BYTEBUFFER = 8
- class TimeKeeper
Bases:
objectTracks elapsed times by id and optionally streams them to the Dragon telemetry infrastructure on a background thread.
A
Telemetryinstance is created internally. When itslevel > 0(i.e. the runtime was launched with--telemetry-levelgreater than zero), a background thread is started that wakes every collection_window seconds, acquires anthreading.RLock, ships every accumulated timing to the telemetry service viaadd_data, resets all accumulators, then releases the lock.timekeeper_namebecomes thets_metric_namein everyadd_datacall (the OpenTSDB metric name).Each timing id is sent as
tagk="id", tagv=str(id).The local hostname is sent as
tagk="hostname", tagv=<hostname>.
When telemetry is inactive (
telemetry.level == 0) no thread is started and no lock is created, preserving the original single-threaded behaviour.- __init__(timekeeper_name: str = '', recording: bool = False, collection_window: float = 10.0, clear_all: bool = True)
- add(id, start, end=None)
- add_elapsed(id, elapsed)
- empty()
- get_timings()
- now()
- record(id, start=None)
- reset(id)
- reset_all()
- start_telemetry()
- class XByteBufferPickler
Bases:
objectA Pickler that has X-language support and is compatible with the C++ SerializableByteBuffer. Pickling a byte buffer with this pickler can be read in C++ by deserializing using the SerializableByteBuffer class. Likewise, a C++ byte buffer serialized with the SerializableByteBuffer can be unpickled into a bytes object as well. This is meant to be used by code that wishes to access the bytes and perhaps provide code that interprets them in an application specific way.
- __init__()
- load(file)
- property ty_val
This is the type value to identify the particular type of value to be unpickled. It is one of the SerType values.
- class XNumPy2DMatrixPickler
Bases:
objectA Pickler that has X-language support and is compatible with the C++ Serializable2DMatrix<Type>. Pickling a numpy array with this pickler can be read in C++ by deserializing using the Serializable2DMatrix<Type>. Likewise, a C++ matrix serialized with the Serializable2DMatrix<Type> can be unpickled into a numpy matrix was well. This is meant to be an efficient implementation for passing 2D matrices to and from other languages (at present that means C++) through a pickling/unpickling interface like native Queue or DDict.
- __init__(data_type: dtype, type_val=<SerType.TY_NONE: 0>)
- byte_size(nparr)
- load(file)
- loads(val)
- property ty_val
- class XNumPyVectorPickler
Bases:
objectA Pickler that has X-language support and is compatible with the C++ SerializableVector<Type>. Pickling a numpy array with this pickler can be read in C++ by deserializing using the SerializableVector<Type>. Likewise, a C++ matrix serialized with the SerializableVector<Type> can be unpickled into a numpy array as well. This is meant to be an efficient implementation for passing arrays/vectors to and from other languages (at present that means C++) through a pickling/unpickling interface like native Queue or DDict.
- __init__(data_type: dtype, type_val=<SerType.TY_NONE: 0>)
- byte_size(nparr)
- load(file)
- loads(val)
- property ty_val
This is the type value to identify the particular type of value to be unpickled. It is one of the SerType values.
- class XPickler
Bases:
objectA Pickler that has X-language support and is compatible with the C++ Serializables. Objects to be shared between Python and C++ must be supported by XPickler on the Python side and the Serializable class on the C++ side. Pickling a supported object with this pickler can be read in C++ by deserializing using the Seralizable class. Likewise, a C++ Serializable object can be unpickled into its corresponding Python object, enabling cross communication between Python and other languages (at present that means C++) through a pickling/unpickling interface like native Queue or DDict.
- __init__()
- choose_pickler(val)
- classmethod ty_bytes(pickler)
- class XScalarPickler
Bases:
objectA Pickler that has X-language support and is compatible with the C++ Scalars like int and double. Pickling a scalar with this pickler can be read in C++ by deserializing using the SeralizableInt or SerializableScalar class. Likewise, a C++ int/double serialized with SeralizableScalar and SerializableScalar can be unpickled into a int/float was well, enabling cross language communication between Python and other languages (at present that means C++) through a pickling/unpickling interface like native Queue or DDict.
- __init__(data_type: dtype, type_val=<SerType.TY_NONE: 0>)
- byte_size(val)
- property ty_val
- class XStringPickler
Bases:
objectA Pickler that has X-language support and is compatible with the C++ SerializableString. Pickling a string with this pickler can be read in C++ by deserializing using the SeralizableString class. Likewise, a C++ string serialized with SeralizableString can be unpickled into a str was well, enabling cross communication between Python and other languages (at present that means C++) through a pickling/unpickling interface like native Queue or DDict.
- __init__()
- byte_size(val)
- property ty_val
- b64decode(the_str)
- b64encode(the_bytes)
- get_cpu_count()
- get_hugepage_mount()
- get_local_kv(key, timeout=None)
- get_local_rt_uid()
- getlasterrstr()
- host_id()
- host_id_from_k8s(pod_uid)
This is used to get a hostid based on the k8s pod uid. It is called when a process in one node needs to know the host_id of another node. It can be called from a node/pod as many times as needed in order to translate a pod uid to a dragon hostid. For example, a backend pod can query the hostids of the other backend pods. For setting/assigning the hostid of the pod itself, each pod needs to call set_host_id().
- set_core_affinity(core)
- set_host_id(new_id)
- set_local_kv(key, value, timeout=None)
- set_procname(name)
- strtobool(val)
Convert a string representation of truth to true (1) or false (0).
True values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’. Raises ValueError if ‘val’ is anything else.