Skip to content

Instantly share code, notes, and snippets.

@schlamar
Last active August 26, 2016 05:56
Show Gist options
  • Save schlamar/7921ec587dd58a72a6d6 to your computer and use it in GitHub Desktop.
Save schlamar/7921ec587dd58a72a6d6 to your computer and use it in GitHub Desktop.
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