'Create a self-pipe for the native thread to synchronize on.
This code is taken from the eventlet tpool module, under terms
of the Apache License v2.0.'

def _init_events_pipe(self):

    self._event_queue = native_Queue.Queue()
    try:
        (rpipe, wpipe) = os.pipe()
        self._event_notify_send = greenio.GreenPipe(wpipe, 'wb', 0)
        self._event_notify_recv = greenio.GreenPipe(rpipe, 'rb', 0)
    except (ImportError, NotImplementedError):
        sock = native_socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('localhost', 0))
        sock.listen(50)
        csock = native_socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        csock.connect(('localhost', sock.getsockname()[1]))
        (nsock, addr) = sock.accept()
        self._event_notify_send = nsock.makefile('wb', 0)
        gsock = greenio.GreenSocket(csock)
        self._event_notify_recv = gsock.makefile('rb', 0)
