Python script embedded in bash, does not exit

假装没事ソ 提交于 2020-01-13 18:21:09

问题


I'm having a curious problem. I have a bash script that is calling a python script within it. The python script executes successfully, but never fully terminates

Content of Bash script:

#! /usr/bin/env bash
python python_script.py
echo "bar"

content of Python script:

#Much stuff
sys.exit("The python script just ended")

What I expect to see on termination would be:

>The python script just ended
>bar

What I instead get is:

>The python script just ended

If I keyboard interrupt, the bash continues as:

^C>bar

What gives? Clearly the exit is calling properly, and there is nothing between that and the output statement in the bash script that called the python script.

(I can't necessarily give specifics on the workings of the "Much stuff" in the python script, as I'm modifying existing code that I don't fully understand. I've mostly left the workings of the script alone, and modified output more than anything for formatting, but I'm happy to try and provide you with any additional information requested)


回答1:


What sys.exit() does is throw an exception of type SystemExit. If your script were to catch this exception, it would continue executing past sys.exit().

Also, if you have non-daemon threads, these could be preventing the process from terminating.

If that's the case, you can either turn them into daemon threads, or somehow signal to them that you wish to exit the script, and let them shut themselves down.

Finally, there's os._exit(), but you should not have to resort to that.



来源:https://stackoverflow.com/questions/10653422/python-script-embedded-in-bash-does-not-exit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!