<s> import ctypes <EOL> import ctypes . wintypes <EOL> import socket <EOL> import sys <EOL> import errno <EOL> from zmq . utils . strtypes import asbytes as b <EOL> SetHandleInformation = ctypes . windll . kernel32 . SetHandleInformation <EOL> SetHandleInformation . argtypes = ( ctypes . wintypes . HANDLE , ctypes . wintypes . DWORD , ctypes . wintypes . DWORD ) <EOL> SetHandleInformation . restype = ctypes . wintypes . BOOL <EOL> HANDLE_FLAG_INHERIT = <NUM_LIT> <EOL> def set_close_exec ( fd ) : <EOL> success = SetHandleInformation ( fd , HANDLE_FLAG_INHERIT , <NUM_LIT:0> ) <EOL> if not success : <EOL> raise ctypes . GetLastError ( ) <EOL> class Waker ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . writer = socket . socket ( ) <EOL> self . writer . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , <NUM_LIT:1> ) <EOL> count = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> count += <NUM_LIT:1> <EOL> a = socket . socket ( ) <EOL> a . bind ( ( "<STR_LIT:127.0.0.1>" , <NUM_LIT:0> ) ) <EOL> connect_address = a . getsockname ( ) <EOL> a . listen ( <NUM_LIT:1> ) <EOL> try : <EOL> self . writer . connect ( connect_address ) <EOL> break <EOL> except socket . error as detail : <EOL> if detail [ <NUM_LIT:0> ] != errno . WSAEADDRINUSE : <EOL> raise <EOL> if count >= <NUM_LIT:10> : <EOL> a . close ( ) <EOL> self . writer . close ( ) <EOL> raise socket . error ( "<STR_LIT>" ) <EOL> a . close ( ) <EOL> self . reader , addr = a . accept ( ) <EOL> self . reader . setblocking ( <NUM_LIT:0> ) <EOL> self . writer . setblocking ( <NUM_LIT:0> ) <EOL> a . close ( ) <EOL> self . reader_fd = self . reader . fileno ( ) <EOL> def fileno ( self ) : <EOL> return self . reader . fileno ( ) <EOL> def wake ( self ) : <EOL> try : <EOL> self . writer . send ( b ( "<STR_LIT:x>" ) ) <EOL> except IOError : <EOL> pass <EOL> def consume ( self ) : <EOL> try : <EOL> while True : <EOL> result = self . reader . recv ( <NUM_LIT> ) <EOL> if not result : break <EOL> except IOError : <EOL> pass <EOL> def close ( self ) : <EOL> self . reader . close ( ) <EOL> self . writer . close ( ) </s>
