Son*_*s.S 5 python multithreading exception
我有一个使用 git lib 的 python 代码。对于某些命令,git lib 会抛出异常(CommandError 异常)。问题是这个异常是从另一个线程抛出的。我确实尝试/例外并捕获了异常并做了一些解决方法。然而,另一个线程仍在打印异常错误消息。
这是git lib中的代码
def pump_stream(cmdline, name, stream, is_decode, handler):
try:
for line in stream:
if handler:
if is_decode:
line = line.decode(defenc)
handler(line)
except Exception as ex:
log.error("Pumping %r of cmd(%s) failed due to: %r", name, cmdline, ex)
raise CommandError(['<%s-pump>' % name] + cmdline, ex) from ex
finally:
stream.close()
Run Code Online (Sandbox Code Playgroud)
它创建这样的线程:
threads = []
for name, stream, handler in pumps:
t = threading.Thread(target=pump_stream,
args=(cmdline, name, stream, decode_streams, handler))
t.setDaemon(True)
t.start()
threads.append(t)
## FIXME: Why Join?? Will block if `stdin` needs feeding...
#
for t in threads:
t.join()
if finalizer:
return finalizer(process)
Run Code Online (Sandbox Code Playgroud)
这是我写的:
try:
<do something in git that will raise the CommandError exception>
except git.exc.CommandError:
print('>>>>>>>>>>>>>>>> I GOT IT')
Run Code Online (Sandbox Code Playgroud)
这是终端上的输出:
Exception in thread Thread-796:
Traceback (most recent call last):
File "/my/src/lib/git/cmd.py", line 88, in pump_stream
raise CommandError('<error from the command>')
git.exc.CommandError: Cmd('<the command>') failed!
cmdline: <the command>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/grid/common/pkgs/python/v3.7.2/lib/python3.7/threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "/my/src/lib/git/cmd.py", line 91, in pump_stream
raise Exception('<error from the command>')
Exception: <error from the command>
>>>>>>>>>>>>>>>> I GOT IT
<... then it finish the rest of the code>
Run Code Online (Sandbox Code Playgroud)
如何停止打印异常错误消息?
| 归档时间: |
|
| 查看次数: |
547 次 |
| 最近记录: |