def connect()

in awsiot/eventstreamrpc.py [0:0]


    def connect(self, lifecycle_handler: LifecycleHandler) -> Future:
        """
        Asynchronously open a network connection.

        Note that close() MUST be called to end a network connection
        that is open (or in the process of connecting).
        Failure to do so will result in leaked resources.

        Args:
            lifecycle_handler: Handler for events over the course of this
                network connection. See :class:`LifecycleHandler` for more info.
                Handler methods will only be invoked if the connect attempt
                succeeds.

        Returns:
            A Future which completes when the connection succeeds or fails.
            If successful, the Future will contain None.
            Otherwise it will contain an exception explaining the reason
            for failure.
        """
        future = Future()
        future.set_running_or_notify_cancel()  # prevent cancellation
        with self._synced as synced:
            old_closed_future = synced.closed_future
            if synced.state != _ClientState.DISCONNECTED:
                raise RuntimeError("Connection already in progress")
            try:
                synced.current_handler = _ProtocolConnectionHandler(
                    self, lifecycle_handler)
                synced.connect_future = future
                synced.state = _ClientState.CONNECTING_TO_SOCKET
                # start new closed_future
                synced.closed_future = Future()
                synced.closed_future.set_running_or_notify_cancel()
                protocol.ClientConnection.connect(
                    handler=synced.current_handler,
                    host_name=self.host_name,
                    port=self.port,
                    bootstrap=self._bootstrap,
                    socket_options=self._socket_options,
                    tls_connection_options=self._tls_connection_options)
            except Exception as e:
                synced.current_handler = None
                synced.connect_future = None
                synced.closed_future = old_closed_future
                synced.state = _ClientState.DISCONNECTED
                raise e
        return future