mirror of
https://github.com/XuehaiPan/nvitop.git
synced 2026-05-21 06:45:24 -06:00
chore(core/process): use WeakValueDictionary to cache instances
Signed-off-by: Xuehai Pan <XuehaiPan@pku.edu.cn>
This commit is contained in:
parent
38cdf5fbc4
commit
65150a4d52
1 changed files with 5 additions and 4 deletions
|
|
@ -13,6 +13,7 @@ import threading
|
|||
from abc import ABCMeta
|
||||
from types import FunctionType
|
||||
from typing import List, Tuple, Dict, Iterable, Callable, Union, Optional, Type, Any, TYPE_CHECKING
|
||||
from weakref import WeakValueDictionary
|
||||
|
||||
from nvitop.core import host
|
||||
from nvitop.core.libnvml import nvml
|
||||
|
|
@ -130,9 +131,11 @@ class HostProcess(host.Process, metaclass=ABCMeta):
|
|||
>>> p1 = HostProcess(12345)
|
||||
>>> p2 = HostProcess(12345)
|
||||
>>> p1 is p2 # the same instance
|
||||
True
|
||||
|
||||
>>> import copy
|
||||
>>> copy.deepcopy(p1) is p1 # the same instance
|
||||
True
|
||||
|
||||
>>> p = HostProcess(pid=12345)
|
||||
>>> p.cmdline()
|
||||
|
|
@ -156,9 +159,7 @@ class HostProcess(host.Process, metaclass=ABCMeta):
|
|||
"""
|
||||
|
||||
INSTANCE_LOCK = threading.RLock()
|
||||
INSTANCES = {}
|
||||
SNAPSHOT_LOCK = threading.RLock()
|
||||
HOST_SNAPSHOTS = {}
|
||||
INSTANCES = WeakValueDictionary()
|
||||
|
||||
def __new__(cls, pid: Optional[int] = None) -> 'HostProcess':
|
||||
"""Returns the cached instance of ``HostProcess``."""
|
||||
|
|
@ -417,7 +418,7 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi
|
|||
"""
|
||||
|
||||
INSTANCE_LOCK = threading.RLock()
|
||||
INSTANCES = {}
|
||||
INSTANCES = WeakValueDictionary()
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def __new__(cls, pid: int, device: 'Device', # pylint: disable=too-many-arguments
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue