如何停止在某些线程中具有阻塞调用的 python 2.7 程序
How to stop a python 2.7 program that has blocking calls in some threads
我有一个 Python 程序,其中有一些线程会阻塞调用。
例如:
#!/usr/bin/python
import threading, tty, sys, os, signal
# super-awesome thread launcher (re-inventing the wheel because I'm
# too lazy to research what they called this)
class Launch(threading.Thread):
def __init__(self, f):
threading.Thread.__init__(self)
self.f = f
self.start()
def run(self):
self.f()
# structure to hold unprocessed chars
Term_Lock = threading.Lock()
Term_Cond = threading.Condition(Term_Lock)
Term_In = []
# launch a thread to retrieve characters from the terminal
tty.setraw(sys.stdin.fileno())
@Launch
def Stdin_Reader():
while True:
c = sys.stdin.read(1)
with Term_Lock:
Term_In.append(c)
Term_Cond.notify()
# main thread
c = None
with Term_Lock:
Term_Cond.wait(1)
if Term_In:
c = Term_In.pop(0)
if c:
print "You pressed '%s'\r" % c
else:
print "You were too slow!\r"
# Lord have mercy on my soul
os.kill(os.getpid(), signal.SIGKILL)
虽然这个程序工作得很好,但最后的 os.kill()
有点令人不安。我用许多其他语言编程过,以前从未见过这种问题。我对语言发明者删除应该在主线程结束时发生的 _Exit 调用没有问题。但是要从系统 API 中完全隐藏 _Exit,现在很紧张。
确实,我们看到的是关于如何以合理的方式停止程序的基本问题。例如:
Exit a process while threads are sleeping
他们说使用 Python 3.0 守护线程。当 Python 3.0 最终引入通用 2.7 兼容性时,我会牢记这一点。所以下一个最好的主意是停止所有线程:
Is there any way to kill a Thread in Python?
但投票最多的回复基本上是 "don't do that"。好的。以我上面的例子为例。阻止调用 sys.stdin.read()
。我们如何解决这个问题?他们说使用 select()
:
Read file with timeout in Python
不过等一下。 Select 仅适用于文件描述符和超时。如果我想从不使用文件描述符生成数据的程序 and/or 库接收其他输入怎么办?所以我必须创建内存管道之类的?这变得越来越荒谬了。
那么,我是否只需要继续使用 os.kill()
直到 Python 3.0 获得认可?
或者有更好的方法吗?
我认为os._exit(0)
是我想要的:
What is difference between sys.exit(0) and os._exit(0)
它似乎工作得很好。我什至可以把它放在我自己的 Exit()
函数中,它可以做我想做的任何清理工作。
我有一个 Python 程序,其中有一些线程会阻塞调用。 例如:
#!/usr/bin/python
import threading, tty, sys, os, signal
# super-awesome thread launcher (re-inventing the wheel because I'm
# too lazy to research what they called this)
class Launch(threading.Thread):
def __init__(self, f):
threading.Thread.__init__(self)
self.f = f
self.start()
def run(self):
self.f()
# structure to hold unprocessed chars
Term_Lock = threading.Lock()
Term_Cond = threading.Condition(Term_Lock)
Term_In = []
# launch a thread to retrieve characters from the terminal
tty.setraw(sys.stdin.fileno())
@Launch
def Stdin_Reader():
while True:
c = sys.stdin.read(1)
with Term_Lock:
Term_In.append(c)
Term_Cond.notify()
# main thread
c = None
with Term_Lock:
Term_Cond.wait(1)
if Term_In:
c = Term_In.pop(0)
if c:
print "You pressed '%s'\r" % c
else:
print "You were too slow!\r"
# Lord have mercy on my soul
os.kill(os.getpid(), signal.SIGKILL)
虽然这个程序工作得很好,但最后的 os.kill()
有点令人不安。我用许多其他语言编程过,以前从未见过这种问题。我对语言发明者删除应该在主线程结束时发生的 _Exit 调用没有问题。但是要从系统 API 中完全隐藏 _Exit,现在很紧张。
确实,我们看到的是关于如何以合理的方式停止程序的基本问题。例如:
Exit a process while threads are sleeping
他们说使用 Python 3.0 守护线程。当 Python 3.0 最终引入通用 2.7 兼容性时,我会牢记这一点。所以下一个最好的主意是停止所有线程:
Is there any way to kill a Thread in Python?
但投票最多的回复基本上是 "don't do that"。好的。以我上面的例子为例。阻止调用 sys.stdin.read()
。我们如何解决这个问题?他们说使用 select()
:
Read file with timeout in Python
不过等一下。 Select 仅适用于文件描述符和超时。如果我想从不使用文件描述符生成数据的程序 and/or 库接收其他输入怎么办?所以我必须创建内存管道之类的?这变得越来越荒谬了。
那么,我是否只需要继续使用 os.kill()
直到 Python 3.0 获得认可?
或者有更好的方法吗?
我认为os._exit(0)
是我想要的:
What is difference between sys.exit(0) and os._exit(0)
它似乎工作得很好。我什至可以把它放在我自己的 Exit()
函数中,它可以做我想做的任何清理工作。