Jython 如何从线程中停止脚本?
我正在寻找一些将从线程运行但能够终止主脚本的退出代码。它在 Jython 中,但我无法使用 java.lang.System.exit(),因为我仍然希望运行我所在的 Java 应用程序,而 sys.exit() 不起作用。理想情况下,我想输出一条消息然后退出。 我的代码使用 threading.Timer 函数在一段时间后运行函数。在这里,我使用它来结束执行时间超过 1 秒的 for 循环。这是我的代码:
import threading
def exitFunct():
#exit code here
t = threading.Timer(1.0, exitFunct)
t.start()
for i in range(1, 2000):
print i
I'm looking for some exit code that will be run from a thread but will be able to kill the main script. It's in Jython but I can't use java.lang.System.exit() because I still want the Java app I'm in to run, and sys.exit() isn't working. Ideally I would like to output a message then exit.
My code uses the threading.Timer function to run a function after a certain period of time. Here I'm using it to end a for loop that is executing for longer than 1 sec. Here is my code:
import threading
def exitFunct():
#exit code here
t = threading.Timer(1.0, exitFunct)
t.start()
for i in range(1, 2000):
print i
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果必须的话,你可以调用
mainThread.stop()
。但你不应该。这篇文章解释了为什么你要做的事情试图这样做被认为是一个坏主意。
Well, if you had to, you could call
mainThread.stop()
. But you shouldn't.This article explains why what you're trying to do is considered a bad idea.
如果您想终止当前进程并且不关心刷新 IO 缓冲区或重置终端,则可以使用 os._exit() 。
我不知道他们为什么让这件事变得如此困难。
If you want to kill the current process and you don't care about flushing IO buffers or reseting the terminal, you can use
os._exit()
.I don't know why they made this so hard.