Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
zmq + SetConsoleCtrlHandler
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