Jython 如何从线程中停止脚本?

发布于 2024-08-12 06:30:28 字数 376 浏览 3 评论 0原文

我正在寻找一些将从线程运行但能够终止主脚本的退出代码。它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-08-19 06:30:28

好吧,如果必须的话,你可以调用 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.

当梦初醒 2024-08-19 06:30:28

如果您想终止当前进程并且不关心刷新 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文