附:自定义上下文管理器,主要实现__enter__ 和__exit__方法
class Query(object):

def __init__(self,name):
self.name = name

def __enter__(self):
print("Begin")
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type:
print('Error')

else:
print('End')

def query(self):
print('Query info about %s...' % self.name)

with Query('Bob') as p :
p.query()

import psutil

print(psutil.cpu_count(logical=False))
print(psutil.cpu_count())

结果:

Begin
Query info about Bob...
End
2
4

一些小知识:2:双核超线程 ht技术  1个核心虚拟出两个共享资源 ,window 10 里面可以限制核心的  

python psutil 使用和windows 10 设置

 

相关文章:

  • 2022-02-22
  • 2021-06-11
  • 2021-12-09
  • 2021-05-18
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
猜你喜欢
  • 2021-07-17
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
  • 2021-10-31
相关资源
相似解决方案