Created
October 13, 2014 10:05
-
-
Save wangchen/0ddc8ae72809f759e64a to your computer and use it in GitHub Desktop.
asyncore alarm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
import asyncore | |
import socket | |
import signal | |
class SignalDispatcher(asyncore.dispatcher): | |
def __init__(self): | |
asyncore.dispatcher.__init__(self) | |
# 0 for read, 1 for write | |
self.pipefd = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0) | |
self.pipefd[1].setblocking(0) | |
self.set_socket(self.pipefd[0]) | |
signal.signal(signal.SIGALRM, self.handle_signal) | |
def handle_read(self): | |
print 'signal', self.recv(1024) | |
def handle_signal(self, sig, frame): | |
self.pipefd[1].send(str(sig)) | |
sd = SignalDispatcher() | |
signal.alarm(1) | |
asyncore.loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment