python 退出阻塞线程?
在我的代码中,我循环遍历 raw_input()
以查看用户是否已请求退出。 我的应用程序可以在用户退出之前退出,但我的问题是应用程序仍然处于活动状态,直到我输入一个键从阻塞函数 raw_input()
返回。 我可以通过向其发送虚假输入来强制 raw_input()
返回吗? 我可以终止它所在的线程吗? (它拥有的唯一数据是一个名为 wantQuit
的变量)。
In my code I loop though raw_input()
to see if the user has requested to quit. My app can quit before the user quits, but my problem is the app is still alive until I enter a key to return from the blocking function raw_input()
. Can I do to force raw_input()
to return by maybe sending it a fake input? Could I terminate the thread that it's on? (the only data it has is a single variable called wantQuit
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不将线程标记为守护进程呢?
来自 文档:
Why don't you just mark the thread as daemonic?
From the docs:
您可以使用这个超时函数来包装您的函数。 以下是食谱:http://code.activestate.com/recipes/473878/
You can use this time out function that wraps your function. Here's the recipe from: http://code.activestate.com/recipes/473878/
您可以使用非阻塞函数来读取用户输入。
这个解决方案是 Windows 特定的:
在 Unix 中执行类似的操作,一次读取一行,与 Windows 版本逐个字符读取不同(感谢 Aaron Digulla 提供 python 用户论坛的链接):
另请参阅:< a href="http://code.activestate.com/recipes/134892/" rel="nofollow noreferrer">http://code.activestate.com/recipes/134892/
You might use a non-blocking function to read user input.
This solution is windows-specific:
To do something similar in Unix, which reads a line at a time, unlike the windows version reading char by char (thanks to Aaron Digulla for providing the link to the python user forum):
See also: http://code.activestate.com/recipes/134892/
Python 邮件上有一个 帖子list 解释了如何在 Unix 上执行此操作:
There is a post on the Python mailing list which explains how to do this for Unix: