通过pustil模块取pid及对应的pidname

时间:2023-03-08 17:10:47
通过pustil模块取pid及对应的pidname

通过pustil模块取pid及对应的pidname

import psutil
import json

def getpid():
    reslut = psutil.pids()
    return reslut 

def getpidname():
    reslut = {}
    pids = getpid()
    for pid in pids:
        pidname = psutil.Process(pid)
        reslut[pid] = pidname.name()
    return json.dumps(reslut,indent=4)
print getpidname()

结果:

{
    ": "init",
    ": "kthreadd",
    ": "migration/0",
    ": "ksoftirqd/0",
    ": "stopper/0",
    ": "watchdog/0",
    ": "migration/1",
    ": "stopper/1",
    ": "ksoftirqd/1",
    ": "watchdog/1",
    ": "events/0",
    ": "events/1",
    ": "events/0",
    ": "events/1",
    ": "events_long/0",
    ": "events_long/1",
    ": "events_power_ef",
    ": "events_power_ef",
    ": "cgroup",
    ": "khelper",
    ": "netns",
    ": "async/mgr",
    ": "pm",
    ": "sync_supers",
    ": "bdi-default",
    ": "kintegrityd/0",
    ": "kintegrityd/1",
    ": "kblockd/0",
    ": "kblockd/1",
    ": "kacpid",
    ": "kacpi_notify",
    ": "kacpi_hotplug",
    ": "ata_aux",
    ": "ata_sff/0",
    ": "ata_sff/1",
    ": "ksuspend_usbd",
    ": "khubd",
    ": "kseriod",
    ": "md/0",
    ": "md/1",
    ": "md_misc/0",
    ": "md_misc/1",
    ": "linkwatch",
    ": "khungtaskd",
    ": "kswapd0",
    ": "ksmd",
    ": "khugepaged",
    ": "aio/0",
    ": "aio/1",
    ": "crypto/0",
    ": "crypto/1",
    ": "kthrotld/0",
    ": "kthrotld/1",
    ": "pciehpd",
    ": "kpsmoused",
    ": "usbhid_resumer",
    ": "deferwq",
    ": "rsyslogd",
    ": "kdmremove",
    ": "kstriped",
    ": "sshd",
    ": "mysqld_safe",
    ": "scsi_eh_0",
    ": "scsi_eh_1",
    ": "mpt_poll_0",
    ": "mpt/0",
    ": "scsi_eh_2",
    ": "bluetooth",
    ": "jbd2/sda1-8",
    ": "ext4-dio-unwrit",
    ": "vmmemctl",
    ": "jbd2/sda3-8",
    ": "ext4-dio-unwrit",
    ": "kauditd",
    ": "mysqld",
    ": "crond",
    ": "udevd",
    ": "mingetty",
    ": "mingetty",
    ": "mingetty",
    ": "udevd",
    ": "udevd",
    ": "mingetty",
    ": "mingetty",
    ": "mingetty",
    ": "sshd",
    ": "bash",
    ": "flush-8:0",
    ": "python"
}