我想检查特定后台文件中的错误,但标准错误流由前台程序控制,并且不显示问题中文件中的错误.不过,我可以使用logging模块并将输出写入文件.我想知道如何使用它来记录所有异常,错误及其追溯.
我正在从网上下载文件,即使我正在做,它也会失败:
对于查询中的p:
try:
except IOError as e:
print e;
Run Code Online (Sandbox Code Playgroud)
如果有错误,我想记录它,然后继续下一个文件.
在这个循环中,我试图下载一个图像,如果由于某种原因文件名不好,或者网站关闭等等,我想继续使用for循环中的下一个项目.
是否有一个更通用的错误,不会失败并继续处理?
另外,如何将错误记录到文件中?
我正在使用AWS并使用AWS Cloudwatch查看日志。虽然事情在AWS上不会中断,但可以。我只是有这种情况。然后我搜索Traceback并得到线
Traceback (most recent call last):
Run Code Online (Sandbox Code Playgroud)
没有实际的追溯。我有一个有效的结构化日志记录设置(请参阅其他问题),我想以类似的方式获取回溯。
所以代替:
Traceback (most recent call last):
File "/home/math/Desktop/test.py", line 32, in <module>
adf
NameError: name 'adf' is not defined
Run Code Online (Sandbox Code Playgroud)
就像是
{"message": "Traceback (most recent call last):\n File \"/home/math/Desktop/test.py\", line 32, in <module>\n adf\n NameError: name 'adf' is not defined", "lineno": 35, "pathname": "/home/math/Desktop/test.py"}
Run Code Online (Sandbox Code Playgroud)
甚至还可以使用JSON格式的字符串。
我能想到的唯一方法是巨大的try-except块。宠物小精灵风格。有更好的解决方案吗?
我已经用 Python 编程一段时间了,但我的工作主要是创建小型实用程序脚本。我认为 Python 是一种很棒的语言,因为它很有趣,而且可以很容易地编写干净的代码。
然而,有一个我还没有找到解决办法的烦恼:由于它的动态特性,可以从各种来源抛出异常,如果它们没有被捕获,它们将杀死您的程序。对我来说,这对“大型”程序来说是一种痛苦。
在 Java 中(并不是说我比 Python 更了解 Java),编译器实际上静态地强制处理异常,以便捕获所有可能的异常。是否有可能用 Python 实现相同的目标?
我正在使用Android模拟器运行自动化测试,该模拟器使用Python编写的Monkey脚本驱动应用程序.该脚本将文件复制到模拟器上,单击应用程序中的按钮并根据软件在其操作期间触发的活动做出反应.该脚本应该运行几千次循环,所以我循环运行adb工具来复制文件,启动活动,通过调用设备上的getProperty方法来查看软件的反应.参数'am.current.comp.class'.所以这是我的脚本的一个非常简化的版本:
for target in targets:
androidSDK.copyFile(emulatorName, target, '/mnt/sdcard')
# Runs the component
device.startActivity(component='com.myPackage/com.myPackage.myactivity')
while 1:
if device.getProperty('am.current.comp.class') == 'com.myPackage.anotheractivity':
time.sleep(1) # to allow the scree to display the new activity before I click on it
device.touch(100, 100, 'DOWN_AND_UP')
# Log the result of the operation somewhere
break
time.sleep(0.1)
Run Code Online (Sandbox Code Playgroud)
(androidSDK是我编写的一个小类,包含一些实用程序函数,用于使用adb工具复制和删除文件).
有时,脚本会崩溃,例如(我正在遗漏完整的堆栈跟踪)
[com.android.chimpchat.adb.AdbChimpDevice]com.android.ddmlib.ShellCommandUnresponsiveException
Run Code Online (Sandbox Code Playgroud)
要么
[com.android.chimpchat.adb.AdbChimpDevice] Unable to get variable: am.current.comp.class
[com.android.chimpchat.adb.AdbChimpDevice]java.net.SocketException: Software caused connectionabort: socket write error
Run Code Online (Sandbox Code Playgroud)
我已经读过,有时设备的套接字连接变得不稳定,可能需要重启(adb start-server和adb kill-server非常有用).
我遇到的问题是这些工具抛出Java异常(Monkey在Jython中运行),但我不确定这些是如何从我的Python脚本中捕获的.我希望能够确定脚本内部失败的确切原因并恢复情况,以便我可以继续进行迭代(例如重新建立连接?例如,重新初始化我的设备与另一个调用到MonkeyRunner.waitForConnection就足够了吗?).
有任何想法吗?
非常感谢,Alberto
编辑.我想我已经提到我发现有可能在Jython脚本中捕获特定于Java的异常,如果有人需要这个:
from java.net import SocketException
...
try:
... …Run Code Online (Sandbox Code Playgroud) 我有一个场景,exceptions在程序执行过程中可能会引发一些未知的问题,我不能except,而且我希望每次有人exception提出电子邮件时,都应该向我发送一封电子邮件,exceptions因为如果没有正确捕获,程序将终止!
所以我已经阅读了有关python提供atexit模块的内容,但它没有使用,exceptions所以我的问题是,有什么方法可以atexit使用exceptions吗??所以每一个引发的异常和程序终止都应该给我发一封邮件?谢谢
我正在使用ThreadPoolExecutorpythonconcurrent.futures并行抓取结果并将结果写入数据库。这样做时,我意识到如果其中一个线程失败,我将无法获得任何信息。我怎样才能正确地知道哪些线程失败以及为什么失败(因此使用“正常”回溯)?下面是一个最小的工作示例。
import logging
logging.basicConfig(format='%(asctime)s %(message)s',
datefmt='%y-%m-%d %H:%M:%S', level=logging.INFO)
from concurrent.futures import ThreadPoolExecutor
def worker_bee(seed):
# sido is not defined intentionally to break the code
result = seed + sido
return result
# uncomment next line, and you will get the usual traceback
# worker_bee(1)
# ThreadPoolExecutor will not provide any traceback
logging.info('submitting all jobs to the queue')
with ThreadPoolExecutor(max_workers=4) as executor:
for seed in range(0,10):
executor.submit(worker_bee, seed)
logging.info(f'submitted, waiting for threads to finish')
Run Code Online (Sandbox Code Playgroud)
如果我在内部导入日志记录 …
AWS Lambda provides built-in logging configuration as described here so that log records include timestamp and request ID. Request ID is a UUID for a particular invocation of a Lambda function, so it's important to include it in each log record because it allows the user to query for all log records with a given request ID (using CloudWatch Logs Insights), which allows for retrieving log records from a particular Lambda invocation. (It is not sufficient to simply filter by …
我经历了" 在Python中记录未捕获的异常 ".而且,我试过这个:
import web
import sys
app = web.application((
'/test', 'test'), globals())
def test_func(e_type, value, traceback):
print "Handled exception here.."
class test:
def GET(self):
a = 1/0
if __name__ == "__main__":
sys.excepthook = test_func
app.run()
Run Code Online (Sandbox Code Playgroud)
在这里,您可以轻松查看是否有GET /test请求,我故意提出ZerDivisionError.正如我已经覆盖sys.excepthook,我希望方法test_func执行ZeroDivisionError.
然而,这段代码并没有按照预期工作.我观察到,当我尝试覆盖excepthook独立代码(不在web-app中)时,它工作正常.正确调用新方法(overriden).
知道为什么这种不同的行为?
python ×10
logging ×4
exception ×2
aws-lambda ×1
debugging ×1
jython ×1
monkeyrunner ×1
python-2.7 ×1
python-3.x ×1
sys ×1
traceback ×1