1# SPDX-License-Identifier: MIT OR Apache-2.0
2# This file is dual licensed under the terms of the Apache License, Version
3# 2.0, and the MIT License. See the LICENSE file in the root of this
4# repository for complete details.
5
6"""
7Generic utilities.
8"""
9
10from __future__ import annotations
11
12import sys
13
14from contextlib import suppress
15from typing import Any
16
17
18def get_processname() -> str:
19 # based on code from
20 # https://github.com/python/cpython/blob/313f92a57bc3887026ec16adb536bb2b7580ce47/Lib/logging/__init__.py#L342-L352
21 processname = "n/a"
22 mp: Any = sys.modules.get("multiprocessing")
23 if mp is not None:
24 # Errors may occur if multiprocessing has not finished loading
25 # yet - e.g. if a custom import hook causes third-party code
26 # to run when multiprocessing calls import.
27 with suppress(Exception):
28 processname = mp.current_process().name
29
30 return processname