简体   繁体   English

停止python脚本而不杀死python进程

[英]Stop python script without killing the python process

I would like to know if there is a way of programatically stopping a python script execution without killing the process like we do with this code: 我想知道是否有一种方法以编程方式停止python脚本执行而不像我们使用此代码一样杀死进程:

import sys
sys.exit()

It would be the code equivalent to Ctrl+c 这将是相当于Ctrl + c的代码

Define your own exception, 定义自己的例外,

class HaltException(Exception): pass

and wrap the script in 并将脚本包装在中

try:
    # script goes here

    # when you want to stop,
    raise HaltException("Somebody stop me!")

except HaltException as h:
    print(h)
    # now what?

Here is what I've found to work -- staying in the interpreter, while stopping a script. 这是我发现的工作 - 停留在翻译中,同时停止脚本。

# ------------------------------------------------------------------
# Reset so get full traceback next time you run the script and a "real"
# exception occurs
if hasattr (sys, 'tracebacklimit'):
    del sys.tracebacklimit

# ------------------------------------------------------------------
# Raise this class for "soft halt" with minimum traceback.
class Stop (Exception):
    def __init__ (self):
        sys.tracebacklimit = 0

# ==================================================================
# ... script here ...
if something_I_want_to_halt_on:
    raise Stop ()

# ... script continues ...

I had this problem while developing a Sublime Text packages. 我在开发Sublime Text包时遇到了这个问题。 I was trying to stop a Sublime Text Python package, to test something while the package was being reloaded. 我试图阻止Sublime Text Python包,在重新加载包时测试一些东西。

If I call sys.exit() , I kill Sublime Text python interpreter and need to restart Sublime Text. 如果我调用sys.exit() ,我会杀死Sublime Text python解释器并需要重新启动Sublime Text。 But after searching I figured it out the solution is pretty simple, I just need to call raise ValueError() , instead of sys.exit() : 但在搜索之后我发现解决方案非常简单,我只需要调用raise ValueError()而不是sys.exit()

import sys
print(sys.path)

sys.exit()

--> - >

import sys
print(sys.path)

raise ValueError()

This will stop the python script execution right after running print(sys.path) . 这将在运行print(sys.path)后立即停止python脚本执行。 Although it will print a big stack trace. 虽然它会打印出大堆栈的痕迹。 But if you add the instruction sys.tracebacklimit = 1 before raise ValueError() , you reduce the stack trace call to one line: 但是如果在raise ValueError()之前添加指令sys.tracebacklimit = 1 ,则将堆栈跟踪调用减少到一行:

import sys
print(sys.path)

raise ValueError()

--> - >

import sys
print(sys.path)

sys.tracebacklimit = 1
raise ValueError()

Related questions: 相关问题:

  1. Stop running python script without killing the interpreter 停止运行python脚本而不杀死解释器
  2. Manually raising (throwing) an exception in Python 在Python中手动引发(抛出)异常

Surely the simplest solution for the OP is raise KeyboardInterrupt ? 当然,最简单的OP解决方案是raise KeyboardInterrupt

This produces the same effect as ctrl+C in the terminal but can be called anywhere in the script. 这与终端中的ctrl + C产生相同的效果,但可以在脚本中的任何位置调用。 No need for import or further definition. 无需导入或进一步定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM