简体   繁体   English

如何遍历函数并将每个用户添加到字典中? (使用psutils包)

[英]How can I loop through a function and add every user to a dictionary? (using psutils package)

I'm using a package called psutils which lets me get some usage information of users on my platform (RHEL7 box). 我正在使用一个名为psutils的软件包,该软件包可让我获取平台上用户的一些使用信息(RHEL7框)。 The main ones I care about are CPU and Memory, which it does great. 我主要关心的是CPU和内存,它们确实很棒。

I am able to put in a user id, and get their memory usage based on the package's functions that describe in their documentation (and then doing some math on my own to customize it). 我能够输入用户ID,并根据其文档中描述的软件包功能获取其内存使用情况(然后自己做一些数学运算以自定义它)。

I also am able to do a basic loop of all the users currently logged in and get a list of them. 我还能够对当前登录的所有用户进行基本循环,并获取其中的列表。

I'm struggling on trying to combine both of that - ie loop through all of the users, apply the function to each one to get all of their memory usage. 我正在努力尝试将两者结合起来-即遍历所有用户,将函数应用于每个用户以获取其所有内存使用量。

Currently, my code is setup to run 1 user at a time - ie I can search for myself (Rob) and it will spit my memory out. 当前,我的代码设置为一次运行1个用户-即,我可以搜索自己(Rob),这会吐出我的记忆。 Now I'm trying to have it loop through all of them. 现在,我正在尝试使其遍历所有这些对象。

If it helps here are my 2 working, very separate chunks of code: 如果有帮助,这里是我的2个正常工作的非常独立的代码块:

Using PSUTILS to find 1 user at a time memory 使用PSUTILS一次找到1个用户

(For example, me as the user, user=rob) (例如,我作为用户,user = rob)

import psutils
import os

mem = psutil.virtual_memory()

#Get all processes for the user (except this one!)
procs = [p.memory_info().rss for p in psutil.process_iter() if p.username() == user and p.pid != os.getpid()]
user_mem = sum(procs)
user_procs = len(procs)
percent_total = user_mem / mem.total * 100

And then I can see my memory simply with something like 然后我可以像这样简单地看到我的记忆

print(user_mem)

And I'll see (some large number as its in bytes) 我会看到(以字节为单位的大量数字)

1XXXXXXX

However, that's just for me - I could supply a list of all the users and run through it all but I don't know if all of them are logged in at that time. 但是,这只是给我的-我可以提供所有用户的列表并遍历所有用户,但是我不知道当时是否所有用户都已登录。

I found a function in psutils, psutils.users() which returns a list of all the users currently logged in. 我在psutils中找到了一个函数psutils.users() ,该函数返回当前登录的所有用户的列表。

Using PSUTILS to get a list of all users 使用PSUTILS获取所有用户的列表

If I run: 如果我运行:

psutil.users()

I'll get a list like: 我会得到一个像这样的列表:

    [suser(name='person1', terminal='pts/0', host='XX.XXX.XXX.XXX', started=1521547648.0),
 suser(name='person2', terminal='pts/3', host='XX.XXX.XXX.XXX', started=1521625856.0),
 suser(name='person3', terminal='pts/6', host='XX.XXX.XXX.XXX', started=1521737728.0),
 suser(name='person4', terminal='pts/7', host='XX.XXX.XXX.XXX', started=1521742080.0),
 suser(name='person5', terminal='pts/1', host='XX.XXX.XXX.XXX', started=1521729920.0),
 suser(name='person6', terminal='pts/8', host='XX.XXX.XXX.XXX', started=1521721472.0),
 suser(name='person7', terminal='pts/11', host='XX.XXX.XXX.XXX', started=1521721472.0),
 suser(name='person8', terminal='pts/14', host='XX.XXX.XXX.XXX', started=1521550208.0),
 suser(name='person9', terminal='pts/18', host='XX.XXX.XXX.XXX', started=1521731328.0),
 suser(name='person10', terminal='pts/22', host='XX.XXX.XXX.XXX', started=1521730688.0)]

So the problem I'm having now is figuring out a way to grab all of those users and apply the my code above and have each one get their memory usage. 因此,我现在遇到的问题是想出一种方法来吸引所有这些用户,并在上面应用我的代码,然后让每个用户获取其内存使用情况。

Then from there, I'm sure I'll able to figure out the maximum, highest user on my own. 然后,从那里开始,我确定自己将能够找出最大,最高的用户。

But I currently am very lost on trying to get that dictionary of all of the users with the values of each of their memory. 但是我目前对尝试使用每个用户的内存值来获取所有用户的字典非常迷失。

Sounds like all you need is to .map on the output of psutil.users() . 听起来您需要做的就是在psutil.users()的输出上进行.map psutil.users() Create a function that takes a suser , filters psutil.process_iter() the same way you are doing it now for that user, and returns the values you care about. 创建一个使用suser的函数,以与现在对该用户相同的方式过滤psutil.process_iter() ,并返回您关心的值。

ps_users = [suser(name='person2',...)]

def get_memory_for_user(user):
    procs = [p.memory_info().rss
       for p in psutil.process_iter()
       if p.username() == user.name and p.pid != os.getpid()]
    return (user.name, sum(procs), len(procs,)

users_usage = map(get_memory_for_user, ps_users)

This should get you on the right track but I'd create a dictionary and store the users there to iterate through. 这应该使您走上正确的道路,但是我会创建一个字典并将用户存储在那里以进行迭代。 One thing to note, from running psutil.users() on my system, I see my username listed multiple times for each tty. 需要注意的一件事是,从在系统上运行psutil.users()来看,我为每个tty多次列出了用户名。 You'll want to look into if procs is getting all of those. 您将要研究procs是否获得了所有这些功能。

Anyway the code should roughly look like: 无论如何,代码应该大致如下所示:

import psutils
import os

mem = psutil.virtual_memory()

ps_dict = {}

for u in psutil.users():
    ps_dict[u].name = None

for user in ps_dict.keys():
    #Get all processes for the user (except this one!)
    ps_dict[user] = {
        'procs':[p.memory_info().rss for p in psutil.process_iter() if p.username() == user and p.pid != os.getpid()],
        'user_mem' : sum(procs),
        'user_procs' : len(procs),
        'percent_total' : user_mem / mem.total * 100
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过循环将变量添加到字典中? - How can I add variables into a dictionary with a loop? 如何使用for循环和带用户输入的if语句在python字典中的键/值之间循环? - How do I cycle through keys/values in a python dictionary using a for loop and if statement with user input? 如何遍历字典,并使用键应用 function:值对作为 arguments - How do I loop through a dictionary, and apply a function using key: value pairs as arguments 我怎样才能让我的功能遍历字典并在用户输入时检查键是否存在 - how can i get my function to go through the dictionary and check if the key exists when user inputs 如何遍历python字典并替换子字符串值? - How can I loop through a python dictionary and replace a substring value? 如何遍历字典以更新并返回字典? - How can I loop through a dictionary to update it and return it? 如何在Python的字典的每个值中添加/减去一个整数? - How can I add/substract an integer to every value of an dictionary in Python? 我怎样才能遍历这个字典而不是硬编码键 - How can I loop through this dictionary instead of hardcoding the keys 如何使用 append 函数或其他函数将字典添加到列表中? - How can i add the dictionary into list using append function or the other function? 如何循环遍历函数参数? - How can I loop through function arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM