cpu挖礦python
① 運行python 的時候為什麼出現了 cpu 使用超過 100%的情況
意思是有python進程沒有結束,IDLE無法啟動
打開任務管理器,找到pythonw.exe進程,結束掉就好了
還是不行的話,這樣:
那你啟動IDLE的時候加一個-n的參數,可以不啟動subprocess
例如這樣:
C:\Python26\Lib\idlelib>idle.bat -n
就可以了
你試試看你的python,都有個idle.bat文件
② Python 怎樣獲取當前計算機的 cpu,內存等信息
用psutil包
cpu:
>>>importpsutil
>>>psutil.cpu_times()
scputimes(user=3961.46,nice=169.729,system=2150.659,idle=16900.540,iowait=629.59,irq=0.0,softirq=19.42,steal=0.0,guest=0,nice=0.0)
>>>
>>>forxinrange(3):
...psutil.cpu_percent(interval=1)
...
4.0
5.9
3.8
>>>
>>>forxinrange(3):
...psutil.cpu_percent(interval=1,percpu=True)
...
[4.0,6.9,3.7,9.2]
[7.0,8.5,2.4,2.1]
[1.2,9.0,9.9,7.2]
>>>
>>>
>>>forxinrange(3):
...psutil.cpu_times_percent(interval=1,percpu=False)
...
scputimes(user=1.5,nice=0.0,system=0.5,idle=96.5,iowait=1.5,irq=0.0,softirq=0.0,steal=0.0,guest=0.0,guest_nice=0.0)
scputimes(user=1.0,nice=0.0,system=0.0,idle=99.0,iowait=0.0,irq=0.0,softirq=0.0,steal=0.0,guest=0.0,guest_nice=0.0)
scputimes(user=2.0,nice=0.0,system=0.0,idle=98.0,iowait=0.0,irq=0.0,softirq=0.0,steal=0.0,guest=0.0,guest_nice=0.0)
>>>
>>>psutil.cpu_count()
4
>>>psutil.cpu_count(logical=False)
2
>>>
內存:
>>>psutil.virtual_memory()
svmem(total=8374149120L,available=2081050624L,percent=75.1,used=8074080256L,free=300068864L,active=3294920704,inactive=1361616896,buffers=529895424L,cached=1251086336)
>>>psutil.swap_memory()
sswap(total=2097147904L,used=296128512L,free=1801019392L,percent=14.1,sin=304193536,sout=677842944)
>>>
③ python有沒有可以獲取每個cpu內核空閑率的
需要安裝psutil庫
importsys
importos
importatexit
importtime
importpsutil
#print"Welcome,currentsystemis",os.name,"3secondslatestarttogetdata..."
time.sleep(3)
line_num=1
#functionofGetCPUState;
defgetCPUstate(interval=1):
return("CPU:"+str(psutil.cpu_percent(interval))+"%")
#functionofGetMemory
defgetMemorystate():
phymem=psutil.virtual_memory()
line="Memory:%5s%%%6s/%s"%(
phymem.percent,
str(int(phymem.used/1024/1024))+"M",
str(int(phymem.total/1024/1024))+"M"
)
returnline
defbytes2human(n):
"""
>>>bytes2human(10000)
'9.8K'
>>>bytes2human(100001221)
'95.4M'
"""
symbols=('K','M','G','T','P','E','Z','Y')
prefix={}
fori,sinenumerate(symbols):
prefix[s]=1<<(i+1)*10
forsinreversed(symbols):
ifn>=prefix[s]:
value=float(n)/prefix[s]
return'%.2f%s'%(value,s)
return'%.2fB'%(n)
defpoll(interval):
"""."""
tot_before=psutil.net_io_counters()
pnic_before=psutil.net_io_counters(pernic=True)
#sleepsometime
time.sleep(interval)
tot_after=psutil.net_io_counters()
pnic_after=psutil.net_io_counters(pernic=True)
#getcpustate
cpu_state=getCPUstate(interval)
#getmemory
memory_state=getMemorystate()
return(tot_before,tot_after,pnic_before,pnic_after,cpu_state,memory_state)
defrefresh_window(tot_before,tot_after,pnic_before,pnic_after,cpu_state,memory_state):
ifos.name=='nt':
os.system("cls")
else:
os.system("clear")
"""Printstatsonscreen."""
#printcurrenttime#cpustate#memory
print(time.asctime()+"|"+cpu_state+"|"+memory_state)
#totals
print("NetStates:")
print("totalbytes:sent:%-10sreceived:%s"%(bytes2human(tot_after.bytes_sent),
bytes2human(tot_after.bytes_recv))
)
print("totalpackets:sent:%-10sreceived:%s"%(tot_after.packets_sent,
tot_after.packets_recv)
)
#per-networkinterfacedetails:let'ssortnetworkinterfacesso
#
print("")
nic_names=pnic_after.keys()
#nic_names.sort(key=lambdax:sum(pnic_after[x]),reverse=True)
fornameinnic_names:
stats_before=pnic_before[name]
stats_after=pnic_after[name]
templ="%-15s%15s%15s"
print(templ%(name,"TOTAL","PER-SEC"))
print(templ%(
"bytes-sent",
bytes2human(stats_after.bytes_sent),
bytes2human(stats_after.bytes_sent-stats_before.bytes_sent)+'/s',
))
print(templ%(
"bytes-recv",
bytes2human(stats_after.bytes_recv),
bytes2human(stats_after.bytes_recv-stats_before.bytes_recv)+'/s',
))
print(templ%(
"pkts-sent",
stats_after.packets_sent,
stats_after.packets_sent-stats_before.packets_sent,
))
print(templ%(
"pkts-recv",
stats_after.packets_recv,
stats_after.packets_recv-stats_before.packets_recv,
))
print("")
try:
interval=0
while1:
args=poll(interval)
refresh_window(*args)
interval=1
except(KeyboardInterrupt,SystemExit):
pass
④ python 怎麼獲得cpu使用率
python獲取代碼:
#!/usr/bin/python
#-*-coding:utf8-*-
__author__='chenwx'
defcpu_rate():
importtime
defcpu_r():
f=open("/proc/stat","r")
forf_lineinf:
break
f.close()
f_line=f_line.split("")
f_line_a=[]
foriinf_line:
ifi.isdigit():
i=int(i)
f_line_a.append(i)
total=sum(f_line_a)
idle=f_line_a[3]
returntotal,idle
total_a,idle_a=cpu_r()
time.sleep(2)
total_b,idle_b=cpu_r()
sys_idle=idle_b-idle_a
sys_total=total_b-total_a
sys_us=sys_total-sys_idle
cpu_a=(float(sys_us)/sys_total)*100
returncpu_a
#printcpu_rate()
⑤ 如何用Python實現cpu和內存的監控
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wmi
import time
def cpu_mem():
c = wmi.WMI()
for processor in c.Win32_Processor():
print ("Processor ID: %s" % processor.DeviceID )
print( "Process Name: %s" % processor.Name.strip() )
for Memory in c.Win32_PhysicalMemory():
print( "Memory Capacity: %.fMB" %(int(Memory.Capacity)/1048576) )
def cpu_use():
c = wmi.WMI()
for i in range(1,3): # while True
for cpu in c.Win32_Processor():
timestamp = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime())
print( ' %s | Utilization: %s: %d %%' % (timestamp, cpu.DeviceID, cpu.LoadPercentage))
time.sleep(3)
def main():
cpu_mem()
cpu_use()
⑥ 怎麼讓python用多個cpu
python由於GIL的關系,python的多線程並沒有發揮多核的作用,這些線程都是在在單核上跑的所以要想發揮多核的作用,就需要使用多進程,盡可能的在每一個CPU核心上分配到一個python進程。所以要想跑滿多核CPU就得多進程多線程互相結合
⑦ 如何讓一個Python的腳本跑滿多核的CPU
python由於GIL的關系,python的多線程並沒有發揮多核的作用,這些線程都是在在單核上跑的
所以要想發揮多核的作用,就需要使用多進程,盡可能的在每一個CPU核心上分配到一個python進程。
所以要想跑滿多核CPU就得多進程多線程互相結合
⑧ python運行時佔用cpu太少
正常的,多核心CPU,一般的python程序就一個進程佔一個核心,8核心CPU,跑滿一個內核也才12.5%的CPU佔用率。部分任務可拆解的,可以寫成多進程python,搜索下 python multipleprocess 就有大把例子。
⑨ aws上cpu利用率怎麼用python採集
#!/usr/bin/python
#-*-coding:utf8-*-
__author__='chenwx'
defcpu_rate():
importtime
defcpu_r():
f=open(「/proc/stat」,「r」)
forf_lineinf:
break
f.close()
f_line=f_line.split(「」)
f_line_a=[]
foriinf_line:
ifi.isdigit():
i=int(i)
f_line_a.append(i)
total=sum(f_line_a)
idle=f_line_a[3]
returntotal,idle
total_a,idle_a=cpu_r()
time.sleep(2)
total_b,idle_b=cpu_r()
sys_idle=idle_b-idle_a
sys_total=total_b-total_a
sys_us=sys_total-sys_idle
cpu_a=(float(sys_us)/sys_total)*100
returncpu_a
#printcpu_rate()
⑩ 如何讓python使用全部cpu
python使用的是単線程的,要想使用全部CPU資源,就需要使用多進程的方式
多進程序就可以使用全部CPU資源了。