def pop(self, n=None):
        """Call from main thread. Returns the list of newly-available (handle, env) pairs."""
        if self._popped:
            assert n is None
            return []
        self._popped = True

        envs = []
        for i, instance in enumerate(self.instances):
            env = remote.Remote(
                handle=self._handles[i],
                vnc_address='{}:{}'.format(instance.host, instance.vnc_port),
                vnc_password='openai',
                rewarder_address='{}:{}'.format(instance.host, instance.rewarder_port),
                rewarder_password='openai',
            )
            envs.append(env)
        return envs