Last active
August 26, 2016 05:56
-
-
Save schlamar/7921ec587dd58a72a6d6 to your computer and use it in GitHub Desktop.
zmq + SetConsoleCtrlHandler
This file contains 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
from ctypes import WINFUNCTYPE, windll | |
from ctypes.wintypes import BOOL, DWORD | |
import zmq | |
kernel32 = windll.LoadLibrary('kernel32') | |
PHANDLER_ROUTINE = WINFUNCTYPE(BOOL, DWORD) | |
SetConsoleCtrlHandler = kernel32.SetConsoleCtrlHandler | |
SetConsoleCtrlHandler.argtypes = (PHANDLER_ROUTINE, BOOL) | |
SetConsoleCtrlHandler.restype = BOOL | |
CTRL_C_EVENT = 0 | |
@PHANDLER_ROUTINE | |
def console_handler(ctrl_type): | |
if ctrl_type == CTRL_C_EVENT: | |
print 'ctrl + c' | |
return False | |
def _add_handler(): | |
if not SetConsoleCtrlHandler(console_handler, True): | |
raise RuntimeError('SetConsoleCtrlHandler failed.') | |
def main(): | |
_add_handler() | |
ctx = zmq.Context() | |
rep = ctx.socket(zmq.REP) | |
rep.bind("tcp://*:5556") | |
rep.recv() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment