2

The following Python:

p=sh.sleep(100, _bg=True)

try:
  p.kill()
except sh.SignalException_SIGKILL:
  print('foo')

Gives me:

>>> Exception in thread background thread for pid 14892:
Traceback (most recent call last):

<blah blah blag, long stack trace elided> 

sh.SignalException_SIGKILL: 

RAN: /usr/bin/sleep 100

STDOUT:

STDERR:

How can I avoid the background thread logging an error for something I expect to happen?

2
  • Have you imported the logging module by any change? Also, have you looked at the docs? Commented Jul 5, 2017 at 22:07
  • No, I have not imported the logging module. Yes, I have looked at the docs. The code is from an example in the docs. Commented Jul 6, 2017 at 15:32

1 Answer 1

4

In order to catch this exception execute your process with _bg_exec=False and execute p.wait().

In [21]: p = sh.sleep(100, _bg=True, _bg_exc=False)
    ...: try:
    ...:     p.kill()
    ...:     p.wait()
    ...: except sh.SignalException_SIGKILL as err:
    ...:     print("foo")
    ...:
foo
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.