你能解释一下 python 中的以下信号处理行为吗?
我有以下程序:
import socket
import sys
import threading
import signal
class serve(threading.Thread):
def __init__(self):
super(serve, self).__init__()
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = ''
self.port = int(sys.argv[1])
def run(self):
self.s.bind((self.host, self.port))
self.s.listen(1)
conn, addr = self.s.accept()
# Call blocks in the following recv
data = conn.recv(1000000)
conn.close()
self.s.close()
def handler(signum, frame):
print "I am the handler: "
signal.signal(signal.SIGHUP, handler)
background = serve()
background.start()
background.join()
有一个客户端程序连接到此程序但不发送任何数据。问题是当发送 SIGHUP 时,会引发“中断系统调用”异常。知道为什么吗?它发生在 python 2.6+ 和 FreeBSD 上。我怀疑它与 http://bugs.python.org/issue1975 有关。
I have the following program:
import socket
import sys
import threading
import signal
class serve(threading.Thread):
def __init__(self):
super(serve, self).__init__()
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.host = ''
self.port = int(sys.argv[1])
def run(self):
self.s.bind((self.host, self.port))
self.s.listen(1)
conn, addr = self.s.accept()
# Call blocks in the following recv
data = conn.recv(1000000)
conn.close()
self.s.close()
def handler(signum, frame):
print "I am the handler: "
signal.signal(signal.SIGHUP, handler)
background = serve()
background.start()
background.join()
There is a client program that connects to this but does not send any data. The problem is when a SIGHUP is sent, and "Interrupted System call" exception is being thrown. Any idea why? It is happening in python 2.6+ and on FreeBSD. I suspect it is related to http://bugs.python.org/issue1975.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果信号到达时正在执行系统调用,则系统调用为 中断。我相信这部分是为了防止升级攻击,部分是为了在调用信号处理程序时保持进程处于一致状态。它还允许您唤醒挂在系统调用上的进程。
要在处理信号后重新启动系统调用,请使用
signal.siginterrupt< /code>
设置信号处理程序后:
If a system call is executing when a signal arrives, the system call is interrupted. I believe this is in part to prevent escalation attacks and in part to keep the process in a consistent state when the signal handler is invoked. It also would allow you to wake up a process that's hung on a system call.
To instead restart system calls after a signal is handled, use
signal.siginterrupt
after you set a signal handler: