psutil

建议使用哪种Python内存分析器? [关闭]

前提是你 提交于 2019-12-27 19:34:29
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我想知道我的Python应用程序的内存使用情况,并且特别想知道哪些代码块/部分或对象占用了大部分内存。 Google搜索显示商业广告是 Python Memory Validator (仅限Windows)。 开源的是 PySizer 和 Heapy 。 我没有尝试任何人,所以我想知道哪一个是最好的考虑: 提供大部分细节。 我必须对代码进行最少或不做任何更改。 #1楼 由于没有人提到它,我将指向我的模块 memory_profiler ,它能够打印内存使用的逐行报告,并且可以在Unix和Windows上运行(最后一个需要psutil)。 输出不是很详细,但目标是概述代码消耗更多内存的位置,而不是对分配的对象进行详尽的分析。 使用 @profile 函数并使用 -m memory_profiler 标志运行代码后,它将打印逐行报告,如下所示: Line # Mem usage Increment Line Contents ============================================== 3 @profile 4 5.97 MB 0.00 MB def my_func(): 5 13.61 MB 7.64 MB a = [1] * (10 ** 6) 6 166.20 MB 152

末学者笔记--Python函数一玄

我与影子孤独终老i 提交于 2019-12-27 09:52:54
Python 函数 一玄 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段。 函数能提高应用的模块性,和代码的重复利用率。你已经知道 Python提供了许多内建函数,比如print()。但你也可以自己创建函数,这被叫做用户自定义函数。 一.定义一个函数 1. 规则 : 你可以定义一个由自己想要功能的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 () 。 任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。 函数的第一行语句可以选择性地使用文档字符串 —用于存放函数说明。 函数内容以冒号起始,并且缩进。 return [表达式] 结束函数,选择性地返回一个值给调用方。不带表达式的 return相当于返回 None。 2. 函数两个阶段 : 定义阶段和调用阶段 ( 1 ) 定义阶段 def test(): print(100) # 内存地址 def test(): print(100) print(test) #打印内存地址 》》 <function test at 0x003FB660 > 》》 (2) 调用阶段 , 函数的内存地址加上 () 就是调用 # test() 二. 实参和形参 # 定义函数括号里的一般叫形参 # 调用时括号里传递的参数一般叫实参 # 比如 : def students(age):

psutil

孤者浪人 提交于 2019-12-25 20:42:16
python之psutil学习 在Python中使用psutil这个第三方模块来监控系统状态,通过一两行代码实现系统监控,还可以跨平台使用,支持Linux/UNIX/OSX/Windows等,是系统管理员和运维小伙伴不可或缺的必备模块。 1、获取CPU信息 在监控系统cpu时,多是关心cpu的物理核数,逻辑核数,内核态时间,用户态时间等,服务器这些信息都可以简单的用psutil来获取,使用python交互模式 cpu相关命令 C : \xxx > python Python 3.8 .1 ( tags / v3 . 8.1 : 1 b293b6 , Dec 18 2019 , 23 : 11 : 46 ) [ MSC v . 1916 64 bit ( AMD64 ) ] on win32 Type "help" , "copyright" , "credits" or "license" for more information . >>> import psutil >>> psutil . cpu_count ( ) #逻辑核数 8 >>> psutil . cpu_count ( logical = False ) #物理核数 4 >>> psutil . cpu_times ( ) #cpu时间 scputimes ( user = 802494.25 , system

psutil.AccessDenied Error while trying to load StanfordCoreNLP

喜夏-厌秋 提交于 2019-12-23 18:12:57
问题 I'm trying to load the package StanfordCoreNLP to get the correct parsing for the movie reviews presented in their page (https://nlp.stanford.edu/sentiment/treebank.html): (I'm using MAC) nlp = StanfordCoreNLP("/Users//NLP_models/stanford-corenlp-full-2018-01-31") But get the error: Traceback (most recent call last): File "/Users/anaconda3/lib/python3.6/site-packages/psutil/_psosx.py", line 295, in wrapper return fun(self, *args, **kwargs) File "/Users/anaconda3/lib/python3.6/site-packages

How to get the script path of a python process who's cmdline is [“python”], with no path?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 18:45:11
问题 I'm trying to get and kill all other running python instances of the same script, I found an edge case where the path is not in psutil's cmdline list, when the process is started with ./myscript.py and not python ./myscript.py the script's content is, note the shebang: #!/bin/python import os import psutil import sys import time for proc in psutil.process_iter(): if "python" in proc.name(): print("name", proc.name()) script_path = sys.argv[0] proc_script_path = sys.argv[0] if len(proc.cmdline

How to see a Windows service dependencies with Python?

送分小仙女□ 提交于 2019-12-11 16:13:42
问题 Using the Windows Services console, you can see a service dependencies under Properties > Dependencies. How you can you get the same information with Python? Is there a way to do it with psutil? 回答1: You can use the subprocess module to query sc.exe to get info for your service and then parse out the dependencies information. Something like: import subprocess def get_service_dependencies(service): try: dependencies = [] # hold our dependency list info = subprocess.check_output(["sc", "qc",

系统监测模块

拥有回忆 提交于 2019-12-09 22:16:29
psutil 模块: --》实现系统监控,可跨平台 --psutil = process and system utilities,它不仅可以通过一两行代码实现系统监控,还可以跨平台使用,支持Linux/UNIX/OSX/Windows等 1、获取CPU信息 1)获取CPU的信息 >>> import psutil >>> psutil.cpu_count() # CPU逻辑数量 4 >>> psutil.cpu_count(logical=False) # CPU物理核心 2 # 2说明是双核超线程, 4则是4核非超线程 2)统计CPU的用户/系统/空闲时间 >>> psutil.cpu_times() scputimes(user=10963.31, nice=0.0, system=5138.67, idle=356102.45) 实现类似top命令的CPU使用率 >>> for x in range(10): #显示10次 ... psutil.cpu_percent(interval=1, percpu=True) # 显示间隔为1秒 2、获取内存信息 1)使用psutil获取虚拟内存和交换内存信息 >>> psutil.virtual_memory() svmem(total=8589934592, available=2866520064, percent=66.6,

cpu_percent(interval=None) always returns 0 regardless of interval value PYTHON

江枫思渺然 提交于 2019-12-08 15:57:39
问题 The code always returns 0.0 values, regardless of interval values. import psutil p = psutil.Process() print p.cpu_percent(interval=1) print p.cpu_percent(interval=None) 回答1: This behaviour is documented: When interval is 0.0 or None compares process times to system CPU times elapsed since last call, returning immediately. That means the first time this is called it will return a meaningless 0.0 value which you are supposed to ignore. In this case is recommended for accuracy that this function

measure elapsed time, amount of memory and cpu used by the extern program

老子叫甜甜 提交于 2019-12-08 08:59:32
问题 I'm executing an external program through Python. I want to know what is the best choice for calling the outside program, with subprocess.Popen() or with subprocess.call() . Also, I need to measure elapsed time, the amount of memory and CPU used by the external program. I've heard of psutil , but I don't really know which to choose. 回答1: also I need to measure elapsed time, amount of memory and cpu used by the extern program (I'm going to assume you only need the information available in your

Python script to monitor process and sub-processes

Deadly 提交于 2019-12-08 07:02:57
问题 I have a Python script which monitors a process and its sub-processes for CPU and memory utilization by the process. The script continuously checks whether the process or one of its sub-processes is Active . Once the process and all its sub-processes are in an Inactive state, the Python script exits. So, the problem I am facing here for a specific process is - Process starts Process creates sub-process-1 Process creates sub-process-2 Process creates sub-process-3 ( Active at latest moment)