我制作了一个 python 脚本,它每 2 秒打印和记录一次特定进程的 CPU % 和内存使用情况。
它工作得很好。直到我去休息。(一小时后)当我再次检查时,python 脚本已暂停(或挂起)。当我按下 [Enter] 时,它又像往常一样开始执行。该脚本工作正常 1 小时。然后日志和输出丢失了 1 小时 30 分钟,而不是再次正常工作。
暂停的原因是什么。?
我怎样才能防止它。?
重要笔记:
全码:
import psutil
import re
import math
import traceback
from time import ctime,sleep,localtime,time
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
import sys
def logg(msg):
msg_parsed = str(ctime()) + " : " + str(msg)
with open("logger.txt","a") as ff:
ff.write("\n" …Run Code Online (Sandbox Code Playgroud) 是否可以在 map 函数中使用 yield ?
出于 POC 目的,我创建了一个示例代码段。
# Python 3 (Win10)
from concurrent.futures import ThreadPoolExecutor
import os
def read_sample(sample):
with open(os.path.join('samples', sample)) as fff:
for _ in range(10):
yield str(fff.read())
def main():
with ThreadPoolExecutor(10) as exc:
files = os.listdir('samples')
files = list(exc.map(read_sample, files))
print(str(len(files)), end="\r")
if __name__=="__main__":
main()
Run Code Online (Sandbox Code Playgroud)
我在示例文件夹中有 100 个文件。根据片段 100*10=1000 应该打印。但是,它只打印 100。当我检查它时只打印生成器对象。
有什么变化,它将被打印 1000?
我有一个字符串:
str1 = "abc = def"
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
str2 = "abc = #Abc#"
Run Code Online (Sandbox Code Playgroud)
我在尝试这个:
re.sub("(\w+) = (\w+)",r"\1 = %s" % ("#"+str(r"\1").title()+"#"),str1)
Run Code Online (Sandbox Code Playgroud)
但它返回:(没有完成字符串操作)
"abc = #abc#"
Run Code Online (Sandbox Code Playgroud)
.title()是什么不起作用.我有一个实例方法,它启动另一个类的对象。对于前,
import module_two
class One:
def start(self):
self.two = module_two.Two()
Run Code Online (Sandbox Code Playgroud)
我正在修补我的测试用例中的一个类
@patch('module_one.One', autospec=True)
def test_one(patched_one):
two = patched_one.return_value
# Following method should raise error, but doesn't
two.any_random_non_existing_method()
Run Code Online (Sandbox Code Playgroud)
如前所述,two.any_random_non_existing_method()不会引发任何错误,因为twoMock 对象没有分配任何规范。
如何为two对象指定规格?我正在寻找类似以下片段的内容。
# note: configure_spec actually doesn't exist.!
two.configure_spec(module_two.Two)
two.any_random_non_existing_method() # Error.!
Run Code Online (Sandbox Code Playgroud) 我的 python 代码中有一个重试机制。如果所有尝试都以某种方式失败,我想提出例外
像这样的东西:
last_exc = None
for i in range(3):
try:
raise Exception(i)
except Exception as e:
last_exc = e
else:
raise last_exc
Run Code Online (Sandbox Code Playgroud)
但问题是我没有在日志中得到确切的回溯。我刚刚收到以下消息:
Traceback (most recent call last):
File "snippet.py", line 8, in <module>
raise e
Exception: 2
Run Code Online (Sandbox Code Playgroud)
我期待raise Exception(i)(第 4 行)作为异常中的回溯。该行为仅适用于 python 2.7。
如何设置我正在引发的异常(在上一个异常中引发)的确切回溯?
一点背景
有没有办法限制要执行的测试用例的数量?就像是..
pytest -vv --limit 1
Run Code Online (Sandbox Code Playgroud)
或者
使用conftest.py?
当我在Solaris bash中运行此代码时:
a=false;
b=false;
if [ $a -o $b ] ; then
echo "_True"
else
echo "_False"
fi
Run Code Online (Sandbox Code Playgroud)
结果:
_真正
脚本的输出不应该是假的吗?
如果我将脚本修改为这样的:
a=false;
b=false;
if [ $a = true -o $b = true ] ; then
echo "_True"
else
echo "_False"
fi
Run Code Online (Sandbox Code Playgroud)
结果:
_假
但是编写"$ a = true"并不是一个好的编码习惯.
拜托,我可以知道这里有什么问题吗?什么是解决方案?
在PowerShell中,如果命令返回一个或多个对象的数组,我可以通过以下方法找出该对象的类:
$ab = SampleCommand
$ab[0].getType()
Run Code Online (Sandbox Code Playgroud)
但是,如果命令没有返回任何内容(0值的数组),我怎么能找到命令的默认返回类型?
注意:例如,我正在处理SCOM PowerShell命令,我正在尝试查找默认的返回类类型的命令get-scomscadvisoragent,但由于未在我的实验室设置中配置顾问代理,因此它不返回任何内容.因此,我无法获得返回对象的类.
python ×6
powershell ×2
bash ×1
exception ×1
mocking ×1
output ×1
pytest ×1
python-2.7 ×1
regex ×1
scom ×1
shell ×1
solaris ×1
types ×1
unit-testing ×1
yield ×1