def _fetch_command(self):
        """ Wait for command from the server """
        poll_status = self.command_poller.poll(self.configuration.timeout *
                                               1000)

        if self.configuration.verbosity > 2:
            self.logger.info(
                f"Worker {self.client_id} polling for commands[idle={self.is_idle}].."
            )

        if poll_status:
            self.unsuccessful_poll_count = 0
            message = pb.WorkerCommand()
            message.ParseFromString(self.command_socket.recv())
            if self.configuration.verbosity > 3:
                self.logger.info("Received command {}".format(message.command))
            return message
        else:
            self.unsuccessful_poll_count += 1

            if self.unsuccessful_poll_count >= self.configuration.polling_limit:
                if self.configuration.verbosity > 2:
                    self.logger.info("Polling refresh")
                self._command_socket_refresh()

            return None
Beispiel #2
0
 def _send_wake_up_call(self):
     """ Send a WAKE_UP message to all the clients """
     command = pb.WorkerCommand(
         command=pb.WorkerCommand.WAKE_UP,
         # nonce=numpy_util.random_int64(),
         # nonce=self.last_command_nonce
     )
     # self.last_command_nonce += 1
     self._publish_command(command, remember=False)
Beispiel #3
0
 def send_client_reset(self):
     """ Send worker command to reset the environments if they have been already initialized """
     command = pb.WorkerCommand(
         command=pb.WorkerCommand.RESET_CLIENT,
         # nonce=numpy_util.random_int64(),
         nonce=self.last_command_nonce,
         instance_id=self.instance_id)
     self.last_command_nonce += 1
     self._publish_command(command)
Beispiel #4
0
    def close_environments(self):
        """ Close all child environments """
        if self.is_closed:
            raise ServerClosedException("Environment already closed")

        self.is_closed = True

        command = pb.WorkerCommand(
            command=pb.WorkerCommand.CLOSE,
            # nonce=numpy_util.random_int64()
            nonce=self.last_command_nonce)
        self.last_command_nonce += 1

        self._publish_command(command)

        self.request_socket.close()
        self.command_socket.close()
    def _fetch_command_fast(self):
        """ Wait for command from the server """
        poll_status = self.command_poller.poll(0)

        if self.configuration.verbosity > 2:
            self.logger.info(
                f"Worker {self.client_id} FAST polling for commands[idle={self.is_idle}].."
            )

        if poll_status:
            self.unsuccessful_poll_count = 0
            message = pb.WorkerCommand()
            message.ParseFromString(self.command_socket.recv())
            if self.configuration.verbosity > 3:
                self.logger.info("Received command {}".format(message.command))
            return message
        else:
            return None
Beispiel #6
0
    def send_actions(self, actions):
        """
        Send actions to the environments.
        Wait for their responses.
        """
        if self.is_closed:
            raise ServerClosedException("Environment already closed")

        action_bytes = pickle.dumps(actions)

        command = pb.WorkerCommand(
            command=pb.WorkerCommand.STEP,
            # nonce=numpy_util.random_int64(),
            nonce=self.last_command_nonce,
            actions=action_bytes)
        self.last_command_nonce += 1

        self._publish_command(command)
        self._reset_frame_buffer()
Beispiel #7
0
    def reset_environments(self):
        """
        Reset all underlying client environments.
        Returns an array of observations
        """
        if self.is_closed:
            raise ServerClosedException("Environment already closed")

        command = pb.WorkerCommand(
            command=pb.WorkerCommand.RESET,
            # nonce=numpy_util.random_int64()
            nonce=self.last_command_nonce)

        self.last_command_nonce += 1

        self._publish_command(command)
        self._reset_frame_buffer()

        obs, _, _, _ = self.gather_frames()
        return obs