|
| 1 | +# TEMPORARY WIP FILE TO USE IN PLACE OF NON-ACCESSIBLE common.py from awscrt until a version is cut. |
| 2 | +# DELETE THIS FILE and point to the correct location after crt has released a version with soft deprecation |
| 3 | + |
| 4 | +""" |
| 5 | +Cross-platform library for `awscrt`. |
| 6 | +""" |
| 7 | +from typing import TYPE_CHECKING |
| 8 | +import _awscrt |
| 9 | + |
| 10 | +__all__ = [ |
| 11 | + "get_cpu_group_count", |
| 12 | + "get_cpu_count_for_group", |
| 13 | + "join_all_native_threads", |
| 14 | + "deprecated", |
| 15 | +] |
| 16 | + |
| 17 | +# At type-check time, expose a real symbol so linters/IDEs understand it. |
| 18 | +# At runtime, prefer typing_extensions; fall back to typing (Py3.13+); else no-op. |
| 19 | +if TYPE_CHECKING: |
| 20 | + # Static analysers will always attempt to import deprecated from typing_extensions and |
| 21 | + # fall back to known interpretation of `deprecated` if it fails and appropriately handle |
| 22 | + # the `@deprecated` tags. |
| 23 | + from typing_extensions import deprecated as deprecated |
| 24 | +else: |
| 25 | + _deprecated_impl = None |
| 26 | + try: |
| 27 | + # preferred import of deprecated |
| 28 | + from typing_extensions import deprecated as _deprecated_impl |
| 29 | + except Exception: |
| 30 | + try: |
| 31 | + from typing import deprecated as _deprecated_impl # Python 3.13+ |
| 32 | + except Exception: |
| 33 | + _deprecated_impl = None |
| 34 | + |
| 35 | + def deprecated(msg=None, *, since=None): |
| 36 | + if _deprecated_impl is None: |
| 37 | + def _noop(obj): return obj |
| 38 | + return _noop |
| 39 | + if since is not None: |
| 40 | + try: |
| 41 | + return _deprecated_impl(msg, since=since) |
| 42 | + except TypeError: |
| 43 | + # older typing_extensions doesn't support the 'since' kwarg |
| 44 | + pass |
| 45 | + return _deprecated_impl(msg) |
| 46 | + |
| 47 | + |
| 48 | +def get_cpu_group_count() -> int: |
| 49 | + """ |
| 50 | + Returns number of processor groups on the system. |
| 51 | +
|
| 52 | + Useful for working with non-uniform memory access (NUMA) nodes. |
| 53 | + """ |
| 54 | + return _awscrt.get_cpu_group_count() |
| 55 | + |
| 56 | + |
| 57 | +def get_cpu_count_for_group(group_idx: int) -> int: |
| 58 | + """ |
| 59 | + Returns number of processors in a given group. |
| 60 | + """ |
| 61 | + return _awscrt.get_cpu_count_for_group(group_idx) |
| 62 | + |
| 63 | + |
| 64 | +def join_all_native_threads(*, timeout_sec: float = -1.0) -> bool: |
| 65 | + """ |
| 66 | + Waits for all native threads to complete their join call. |
| 67 | +
|
| 68 | + This can only be safely called from the main thread. |
| 69 | + This call may be required for native memory usage to reach zero. |
| 70 | +
|
| 71 | + Args: |
| 72 | + timeout_sec (float): Number of seconds to wait before a timeout exception is raised. |
| 73 | + By default the wait is unbounded. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + bool: Returns whether threads could be joined before the timeout. |
| 77 | + """ |
| 78 | + return _awscrt.thread_join_all_managed(timeout_sec) |
0 commit comments