Skip to content

Instantly share code, notes, and snippets.

@wangchen
Created October 13, 2014 10:05
Show Gist options
  • Save wangchen/0ddc8ae72809f759e64a to your computer and use it in GitHub Desktop.
Save wangchen/0ddc8ae72809f759e64a to your computer and use it in GitHub Desktop.
asyncore alarm
# 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