- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在测试我使用 rest api 制作的应用程序时,我发现了这种我不理解的行为。
让我们从重现类似的错误开始,如下所示 -
在文件 call.py
-
请注意,此文件包含以视觉方式显示自身的代码,例如一个永远运行的 GUI。在这里,我只是向您展示一个表示,并故意让它引发一个异常来向您展示问题。发出 get 请求然后尝试将结果解析为 json 将引发 JSONDecodeError
。
import requests
from time import sleep
sleep(3)
uri = 'https://google.com'
r = requests.get(uri)
response_dict = r.json()
因为我想将其作为守护进程运行,所以我使用以下技巧将此进程与启动它的终端分离 -
在文件 start.py
-
import subprocess
import sys
subprocess.Popen(["python3", "call.py"])
sys.exit(0)
然后我执行python3 start.py
它显然解耦了流程,因为如果没有异常,视觉表现就会完美运行。
但是,如果出现异常,我会立即在终端中看到此输出,即使我在调用 python3 start.py
-
$ python3 start.py
$ Traceback (most recent call last):
File "call.py", line 7, in <module>
response_dict = r.json()
File "/home/walker/.local/lib/python3.6/site-packages/requests/models.py", line 896, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 518, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
现在,我明白所有异常都必须在程序本身中处理。在这个奇怪的问题之后我已经这样做了,但我不清楚的是为什么一开始会发生这种情况?
如果我退出终端并重新启动终端,则不会发生这种情况(在回溯的情况下,视觉表现会卡住,并且任何终端上都没有预期的输出)
为什么解耦过程会这样?
注意:解耦对我来说是势在必行的。 GUI 作为后台或守护进程运行并且生成它的终端必须从它中解放出来。
最佳答案
通过“解耦”,我假设您的意思是您希望 stdout/stderr 转到 /dev/null
?假设这就是你的意思,那不是你告诉你的代码要做的
来自 docs :
stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively. Valid values are
PIPE
,DEVNULL
, an existing file descriptor (a positive integer), an existing file object, andNone
.With the default settings of
None
, no redirection will occur; the child’s file handles will be inherited from the parent.
因此你可能想做:
from subprocess import Popen, DEVNULL
Popen(["python3", "call.py"], stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL)
根据 OP 的评论,我认为他们可能会使用 GNU screen
或 tmux
之类的工具。像这样的终端多路复用器允许您创建一个虚拟终端,您可以在需要时断开连接并重新连接。这些答案参见 https://askubuntu.com/a/220880/106239和 https://askubuntu.com/a/8657/106239分别有tmux
和screen
的例子
关于python - 从终端解耦的过程仍然输出 Traceback 到终端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53179244/
如何确保在不包括整个回溯的情况下打印出失败的实际行?追溯对我来说可能太长了,所以我也把它全部打印出来。 此代码仅打印函数 a 和 b 中的错误,但我想查看实际错误发生在函数 d 中。 import t
我有以下内容: try: package_info = __import__('app') #app.py except: print traceback.extract_tb(sys
我有 2 个模块: a.py: import b import traceback try: print b.get_val(1) except Exception as ex: tr
我的 Python 脚本崩溃了。为了调试它,我以交互模式运行它 python -i example.py Traceback (most recent call last): File "exam
我刚开始在 rstudio 中进行调试。一开始一切都按照描述的方式工作 here . 我使用后 browser() ,我无法回到这个状态,也就是说没有互动区,我可以在那里按hide traceback
使用 Lua 演示页面中的以下代码,我试图获取被调用函数的名称。 function test() local info = debug.getinfo(1); for k, v in
假设我有一个非常简单的代码会引发错误: print(1/0) 如何将完整的回溯错误保存到文件中,以便该文件包含: Traceback (most recent call last): File "
我正在编写一个自定义错误处理程序(使用error选项),主要用于与source运行的程序一起使用。在自定义错误处理程序中,我想计算导致错误的源文件的行号。 通常,每当引发错误时,都会将基本环境中的.T
我遇到过 R 中 .Traceback 对象的奇怪行为。 当我尝试打印简短的错误消息时,没问题,.Traceback[[1]] 有一个元素。但是当我尝试打印很长的字符串时,.Traceback[[1]
我发现为一个简单的失败的单元测试获取如此多的细节有点烦人。除了实际定义的断言消息之外,是否可以抑制所有内容? Creating test database for alias 'default'...
我有一个简单的脚本: i=1 while True: try: print i except KeyboardInterrupt: raise Exce
我已经开始编码大约一个星期了,在练习创建一个形状计算器时,我遇到了这样的错误: Traceback (most recent call last): File "python", line 4 if
在测试我使用 rest api 制作的应用程序时,我发现了这种我不理解的行为。 让我们从重现类似的错误开始,如下所示 - 在文件 call.py - 请注意,此文件包含以视觉方式显示自身的代码,例如一
我想查看代码到特定点的完整轨迹 我也是 ... import traceback traceback.print_stack() ... 然后会显示 File ".venv/lib/python3
traceback 模块非常适合捕获和处理异常,但在下面的示例中,它似乎从最近的异常中捕获了一个不完整的堆栈。 考虑两个文件,一个是“mymod.py”: import sys, traceback
我正在尝试对回溯进行一些详细的重新检查并获取实际值来自未能返回更多(更好?)信息的对象回溯。 案例场景在我导入并执行的函数中,如下所示: def foo(): a = True b =
是否可以在 Python 中创建自定义回溯?我正在尝试编写一个函数 raise_from() 来模仿 Python 3 的 raise ... from ...。 def raise_from(exc
我在运行代码时遇到了 Mechanize 问题,我不知道问题出在哪里,也许有人可以帮助我。 > ****************************************************
需要帮忙 我想要的是 : 我想录制麦克风并从 txt 文件中获取持续时间 代码: import sounddevice as sd import numpy as np import scipy.io
令我惊讶的是,当我获取文件时,很难在 R 中准确找到错误发生的位置。例如: > source('Data-Generation.R') ... # some output here Error in
我是一名优秀的程序员,十分优秀!