Ejemplo n.º 1
0
    def close(self, name=None, reason=u'closed by RewarderSession.close'):
        if name is None:
            names = list(self.names_by_id.values())
        else:
            logger.info('[%s] Closing rewarder connection', name)
            names = [name]
        self.ids_by_name = {name: id for id, name in self.names_by_id.items()}

        for name in names:
            with self.lock:
                id = self.ids_by_name.pop(name, None)
                if id is None:
                    # already closed
                    continue

                del self.names_by_id[id]
                del self.reward_buffers[id]
                del self.env_statuses[id]
                self.errors.pop(id, None)

                network = self.networks.pop(id)
                network.close()

                client = self.clients.pop(id, None)
                if client is not None:
                    reactor.callFromThread(client.close, reason=reason)
Ejemplo n.º 2
0
    def send_env_reward(self, reward, done, info, episode_id):
        pyprofile.incr('agent_conn.reward', reward)
        if done:
            pyprofile.incr('agent_conn.done')

        reactor.callFromThread(self._send_env_reward, reward, done, info,
                               episode_id)
Ejemplo n.º 3
0
    def listen(self, port=15900):
        logger.info('Starting Rewarder on port=%s', port)
        factory = websocket.WebSocketServerFactory()
        factory.agent_conn = self
        factory.protocol = RewarderProtocol

        reactor.callFromThread(reactor.listenTCP, port, factory)
Ejemplo n.º 4
0
 def send_env_describe(self,
                       env_id,
                       env_state,
                       episode_id,
                       fps,
                       headers=None,
                       parent_message_id=None,
                       parent_context=None):
     reactor.callFromThread(self._send_env_describe, env_id, env_state,
                            episode_id, fps, headers, parent_message_id,
                            parent_context)
Ejemplo n.º 5
0
def blockingCallFromThread(f, *a, **kw):
    local_queue = queue.Queue()

    def _callFromThread():
        result = defer.maybeDeferred(f, *a, **kw)
        result.addBoth(local_queue.put)

    reactor.callFromThread(_callFromThread)
    result = queue_get(local_queue)
    if isinstance(result, failure.Failure):
        if result.frames:
            e = error.Error(str(result))
        else:
            e = result.value
        raise e
    return result
Ejemplo n.º 6
0
    def connect(self,
                name,
                address,
                label,
                password,
                env_id=None,
                seed=None,
                fps=60,
                start_timeout=None,
                observer=False,
                skip_network_calibration=False):
        if name in self.reward_buffers:
            self.close(
                name,
                reason=
                'closing previous connection to reconnect with the same name')

        network = Network()
        self.names_by_id[self.i] = name
        self.reward_buffers[self.i] = reward_buffer.RewardBuffer(label)
        self.env_statuses[self.i] = env_status.EnvStatus(label=label,
                                                         primary=False)
        self.networks[self.i] = network

        reactor.callFromThread(
            self._connect,
            name=name,
            address=address,
            env_id=env_id,
            seed=seed,
            fps=fps,
            i=self.i,
            network=network,
            env_status=self.env_statuses[self.i],
            reward_buffer=self.reward_buffers[self.i],
            label=label,
            start_timeout=start_timeout,
            password=password,
            observer=observer,
            skip_network_calibration=skip_network_calibration,
        )
        self.i += 1
        return network
Ejemplo n.º 7
0
 def send_action(self, action_n, env_id):
     reactor.callFromThread(self._send_action, env_id, action_n)
     return self.pop_errors()
Ejemplo n.º 8
0
 def reset(self, seed=None, env_id=None):
     with self.lock:
         for i, reward_buffer in self.reward_buffers.items():
             reward_buffer.mask()
     reactor.callFromThread(self._reset, seed=seed, env_id=env_id)
Ejemplo n.º 9
0
 def send_reply_env_reset(self, *args, **kwargs):
     reactor.callFromThread(self._send_reply_env_reset, *args, **kwargs)
Ejemplo n.º 10
0
 def send_reply_error(self, *args, **kwargs):
     reactor.callFromThread(self._send_reply_error, *args, **kwargs)
Ejemplo n.º 11
0
 def send_env_observation(self, observation, episode_id):
     reactor.callFromThread(self._send_env_observation, observation,
                            episode_id)
Ejemplo n.º 12
0
 def send_env_text(self, text, episode_id):
     ''' text channel to communicate with the agent '''
     reactor.callFromThread(self._send_env_text, text, episode_id)
Ejemplo n.º 13
0
 def step(self, action):
     reactor.callFromThread(self._step, action)
     return self.flip()