[Python] How to run psutil inside a docker container?

Discussão em 'Python' iniciado por Stack, Julho 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to monitor the CPU and Memory of my docker container from within my python application . I am planning to use psutil for this job. I have read on other forums that the /proc can be changed in psutil.

    import psutil
    psutil.PROCFS_PATH = 'proc/self'
    psutil.cpu_percent()


    This gives me the following error:

    File "app.py", line 22, in <module>
    web-vote-app_1 | r = psutil.cpu_percent()
    web-vote-app_1 | File "/usr/local/lib/python2.7/site-packages/psutil /__init__.py", line 1773, in cpu_percent
    web-vote-app_1 | _last_cpu_times = cpu_times()
    web-vote-app_1 | File "/usr/local/lib/python2.7/site-packages/psutil/__init__.py", line 1645, in cpu_times
    web-vote-app_1 | return _psplatform.cpu_times()
    web-vote-app_1 | File "/usr/local/lib/python2.7/site-packages/psutil/_pslinux.py", line 544, in cpu_times
    web-vote-app_1 | fields = [float(x) / CLOCK_TICKS for x in fields]
    web-vote-app_1 | ValueError: could not convert string to float: (python)
    web-result_1 | * Running on http://0.0.0.0:5002/ (Press CTRL+C to quit)
    swarmmicroservicedemov1_web-vote-app_1 exited with code 1


    Can anyone tell me if its possible to get the same information about the container instead of the host, if so. how ? Thanks

    Continue reading...

Compartilhe esta Página