[英]Python ignores SIGINT in multithreaded programs - how to fix that?
I have Python 2.6 on MacOS X and a multithread operation. 我在MacOS X上有Python 2.6和多线程操作。 Following test code works fine and shuts down app on Ctrl-C:
以下测试代码正常工作并关闭Ctrl-C上的应用程序:
import threading, time, os, sys, signal
def SigIntHandler( signum, frame ) :
sys.exit( 0 )
signal.signal( signal.SIGINT, SigIntHandler )
class WorkThread( threading.Thread ) :
def run( self ) :
while True :
time.sleep( 1 )
thread = WorkThread()
thread.start()
time.sleep( 1000 )
But if i change only one string, adding some real work to worker thread, the app will never terminate on Ctrl-C: 但是,如果我只更改一个字符串,向工作线程添加一些实际工作,应用程序永远不会在Ctrl-C上终止:
import threading, time, os, sys, signal
def SigIntHandler( signum, frame ) :
sys.exit( 0 )
signal.signal( signal.SIGINT, SigIntHandler )
class WorkThread( threading.Thread ) :
def run( self ) :
while True :
os.system( "svn up" ) # This is really slow and can fail.
time.sleep( 1 )
thread = WorkThread()
thread.start()
time.sleep( 1000 )
Is it possible to fix it, or python is not intended to be used with threading? 是否可以修复它,或者python不适合与线程一起使用?
I'm not an expert on Threads with Python but quickly reading the docs leads to a few conclusions. 我不是Python的线程专家,但很快阅读文档会得出一些结论。
1) Calling os.system()
spawns a new subshell and is not encouraged. 1)调用
os.system()
产生一个新的子shell,不鼓励。 Instead the subprocess module should be used. 而应该使用子进程模块。 http://docs.python.org/release/2.6.6/library/os.html?highlight=os.system#os.system
http://docs.python.org/release/2.6.6/library/os.html?highlight=os.system#os.system
2) The threading
module doesn't seem to give a whole lot of control to the threads, maybe try using the thread
module, at least there is a thread.exit()
function. 2)
threading
模块似乎没有给threading
提供大量的控制,也许尝试使用thread
模块,至少有一个thread.exit()
函数。 Also from the threading
docs here it says that dummy threads may be created, which are always alive and daemonic, furthermore 同样来自这里的
threading
文档,它表示可以创建虚拟线程,此外,它们总是存活的和守护的
"… the entire Python program exits when only daemon threads are left."
So, I would imagine that the least you need to do is signal the currently running threads that they need to exit, before exiting the main thread, or joining them on ctrl-c to allow them to finish (although this would obviously be contradictory to ctrl-c), or perhaps just using the subprocess
module to spawn the svn up
would do the trick. 因此,我认为你需要做的最少是在退出主线程之前发出当前正在运行的线程的信号,或者在ctrl-c上加入它们以允许它们完成(尽管这显然是矛盾的ctrl-c),或者只是使用
subprocess
模块来生成svn up
就可以了。
A couple of things which may be causing your problem: 可能导致您的问题的一些事情:
svn
, which is ignoring it. svn
捕获,而忽略了它。 join()
it before exiting. join()
它。 While it always seems to stop on my Linux system, MacOS X behaviour may be different. Python works well enough with threads :-) Python与线程运行良好:-)
Update: You could try using subprocess
, setting up the child process so that file handles are not inherited, and setting the child's stdin to subprocess.PIPE. 更新:您可以尝试使用
subprocess
进程,设置子进程以便不继承文件句柄,并将子进程的stdin设置为subprocess.PIPE。
You likely do not need threads at all. 你可能根本不需要线程。
Try using Python's subprocess
module, or even Twisted's process support . 尝试使用Python的
subprocess
进程模块,甚至是Twisted的进程支持 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.