File: process_monitor.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (73 lines) | stat: -rw-r--r-- 2,793 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
import psutil
import threading
import time

from logger import get_base_logger


class ProcessMonitor:
    def __init__(self, log_filename, logger_name, log_interval=5.0, print_console=False, process_id=None, **kwargs):
        """
        Process Monitor monitors the CPU usage, memory usage of a specific process.

        :param logger_filename: The log filename for the logger.
        :param logger_name: The name for the logger.
        :param log_interval: The interval of logging.
        :param print_console: Whether output log content to console. Default is False.
        :param process_id: The process id of the process if monitor, if this is set None, then it will monitor
         the process that the ProcessMonitor is running in.
        """
        self._monitor_thread = None
        self._logger = get_base_logger(
            log_filename=log_filename, logger_name=logger_name, print_console=print_console, **kwargs
        )
        self._pid = process_id or os.getpid()
        self._process_instance = psutil.Process(self._pid)
        self._log_interval = log_interval
        self.running = False

    def __enter__(self):
        print("Process monitor start working.")
        self.start()
        return self

    def __exit__(self, *args):
        self.stop()
        print("Process monitor stop working.")

    def _monitor_work(self):
        while self.running:
            log_content = "process status: %s, process cpu usage percent: %s, process memory usage percent: %.3f"
            self._logger.info(
                log_content,
                self._process_instance.status(),
                self._process_instance.cpu_percent(),
                self._process_instance.memory_percent(),
            )
            time.sleep(self._log_interval)

    @property
    def memory_usage_percent(self):
        return self._process_instance.memory_percent() * 100

    @property
    def cpu_usage_percent(self):
        return self._process_instance.cpu_percent()

    def start(self):
        self.running = True
        self._monitor_thread = threading.Thread(target=self._monitor_work, daemon=True)
        self._monitor_thread.start()
        self._logger.info("Start monitoring process id: %d", self._pid)

    def stop(self):
        self.running = False
        self._monitor_thread.join()
        self._logger.ino("Stop monitoring process id: %d", self._pid)
        self._monitor_thread = None