Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading Python 2.7中的线程安全信号API_Multithreading_Python 2.7_Signals - Fatal编程技术网

Multithreading Python 2.7中的线程安全信号API

Multithreading Python 2.7中的线程安全信号API,multithreading,python-2.7,signals,Multithreading,Python 2.7,Signals,我有一个多线程程序,想捕获SIGINT。我尝试使用信号接口,但当程序使用线程时,它似乎停止了任何操作。多线程python程序的正确信号API是什么 import signal import sys import threading import os # register my Ctrl-C handler def signal_handler(signal, frame): print('Signal.') os._exit(0) signal.signal(

我有一个多线程程序,想捕获SIGINT。我尝试使用信号接口,但当程序使用线程时,它似乎停止了任何操作。多线程python程序的正确信号API是什么

import signal
import sys
import threading
import os

# register my Ctrl-C handler        
def signal_handler(signal, frame):
    print('Signal.')
    os._exit(0)
signal.signal(signal.SIGINT, signal_handler)

# stop the main thread
lock = threading.Lock()
with lock:
    threading.Condition(lock).wait()
我使用的是python 2.7.6,我的pip freeze说:

我还尝试在主线程中执行signal.pause,但仍然不起作用:

import threading, sys, signal, os

stderr_lock = threading.Lock()

def Log(module, msg):
    with stderr_lock:
        sys.stderr.write("%s: %s\n" % (module, msg))

class My_Thread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        Log("Init", "Initing.")
        self.start()
    def run(self):
        while True:
            Log("Run", "Running.")

for i in range(100):
    My_Thread()

# trap ctrl-C in main thread
def handler(signal, frame):
    print "GOT SIGINT!"
    os._exit(0)
signal.signal(signal.SIGINT, handler)
signal.pause()

奇怪的是,如果将线程数从100减少到87,它通常会起作用。

只有主线程在侦听SIGINT。确保所有线程都在侦听SIGINT值。

尝试了该操作,但get:ValueError:signal仅在主线程中有效。因此,您只能在主线程中调用signal.signal。此外,signal.signal的官方2.7文档表示,当线程被启用时,此函数只能从主线程调用;尝试从其他线程调用它将导致引发ValueError异常。我感谢您的贡献。你为什么拒绝我的编辑?如果你有信号的话,一切似乎都正常。在主线程中暂停,然后重试。除了在所有其他线程中。尽管我确实注意到,在某些情况下,如果在早期解析阶段出现异常,信号就会丢失。也许解决方案是让init.py导入try块中的所有内容。虽然如果Python只是有一个不中断ctrl-C CLEASE标志会更好。看起来以前遇到过这个问题:。有一个建议的解决方案定义了一个InterruptableThread。问题是您没有在try块中包含线程创建。关于更详细的解释,请参见我对另一个问题的回答。@JohanL。在捕捉信号的背景下。。。这里的观察结果是,如果我们有线程,我们不能仅仅依赖于信号接口。我们也必须抓住例外。见我对托比答案的评论。
import threading, sys, signal, os

stderr_lock = threading.Lock()

def Log(module, msg):
    with stderr_lock:
        sys.stderr.write("%s: %s\n" % (module, msg))

class My_Thread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        Log("Init", "Initing.")
        self.start()
    def run(self):
        while True:
            Log("Run", "Running.")

for i in range(100):
    My_Thread()

# trap ctrl-C in main thread
def handler(signal, frame):
    print "GOT SIGINT!"
    os._exit(0)
signal.signal(signal.SIGINT, handler)
signal.pause()