Python:当陷入阻塞 raw_input 时如何退出 CLI?

发布于 2024-10-10 09:19:35 字数 358 浏览 2 评论 0原文

我有一个 GUI 程序,也应该可以通过 CLI 进行控制(用于监控)。 CLI 是使用 raw_input 在 while 循环中实现的。 如果我通过 GUI 关闭按钮退出程序,它会挂在 raw_input 中,并且在获得输入之前不会退出。

如何在不输入输入的情况下立即中止 raw_input?

我在 WinXP 上运行它,但我希望它独立于平台,它也应该在 Eclipse 中工作,因为它是一个开发工具。 Python版本是2.6。

我搜索了 stackoverflow 几个小时,我知道这个主题有很多答案,但是真的没有平台独立的解决方案来拥有非阻塞的 CLI 阅读器吗?

如果没有,解决这个问题的最佳方法是什么?

谢谢

I have a GUI program which should also be controllable via CLI (for monitoring). The CLI is implemented in a while loop using raw_input.
If I quit the program via a GUI close button, it hangs in raw_input and does not quit until it gets an input.

How can I immediately abort raw_input without entering an input?

I run it on WinXP but I want it to be platform independent, it should also work within Eclipse since it is a developer tool. Python version is 2.6.

I searched stackoverflow for hours and I know there are many answers to that topic, but is there really no platform independent solution to have a non-blocking CLI reader?

If not, what would be the best way to overcome this problem?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦明 2024-10-17 09:19:35

这可能不是最好的解决方案,但您可以使用 thread 模块 它具有以下功能 thread.interrupt_main()。因此可以运行两个线程:一个使用 raw_input 方法,另一个可以发出中断信号。上层线程引发 KeyboardInterrupt 异常。

import thread
import time

def main():
    try:
        m = thread.start_new_thread(killable_input, tuple())
        while 1:
            time.sleep(0.1) 
    except KeyboardInterrupt:
        print "exception" 

def killable_input():
    w = thread.start_new_thread(normal_input, tuple())
    i = thread.start_new_thread(wait_sometime, tuple())


def normal_input():
    s = raw_input("input:")


def wait_sometime():
    time.sleep(4) # or any other condition to kill the thread
    print "too slow, killing imput"
    thread.interrupt_main()

if __name__ == '__main__':
    main()

That's not maybe the best solution but you could use the thread module which has a function thread.interrupt_main(). So can run two thread : one with your raw_input method and one which can give the interruption signal. The upper level thread raise a KeyboardInterrupt exception.

import thread
import time

def main():
    try:
        m = thread.start_new_thread(killable_input, tuple())
        while 1:
            time.sleep(0.1) 
    except KeyboardInterrupt:
        print "exception" 

def killable_input():
    w = thread.start_new_thread(normal_input, tuple())
    i = thread.start_new_thread(wait_sometime, tuple())


def normal_input():
    s = raw_input("input:")


def wait_sometime():
    time.sleep(4) # or any other condition to kill the thread
    print "too slow, killing imput"
    thread.interrupt_main()

if __name__ == '__main__':
    main()
岁月如刀 2024-10-17 09:19:35

根据您使用的 GUI 工具包,找到一种方法将事件侦听器连接到关闭窗口操作并使其调用 win32api.TerminateProcess(-1, 0)

作为参考,在 Linux 上调用 sys.exit()< /a> 有效。

Depending on what GUI toolkit you're using, find a way to hook up an event listener to the close window action and make it call win32api.TerminateProcess(-1, 0).

For reference, on Linux calling sys.exit() works.

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