def _maintain_hosting_device_pool(self, context, template):
<EOL>lock = self._get_template_pool_lock(template['<STR_LIT:id>'])<EOL>acquired = lock.acquire(False)<EOL>if not acquired:<EOL><INDENT>return<EOL><DEDENT>try:<EOL><INDENT>capacity = template['<STR_LIT>']<EOL>if capacity == <NUM_LIT:0>:<EOL><INDENT>return<EOL><DEDENT>desired = template['<STR_LIT>']<EOL>available = self._get_total_available_slots(<EOL>context, template['<STR_LIT:id>'], capacity)<EOL>grow_threshold = abs(desired - capacity)<EOL>if available <= grow_threshold:<EOL><INDENT>num_req = int(math.ceil(grow_threshold / (<NUM_LIT:1.0> * capacity)))<EOL>num_created = len(self._create_svc_vm_hosting_devices(<EOL>context, num_req, template))<EOL>if num_created < num_req:<EOL><INDENT>LOG.warning('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>{'<STR_LIT>': num_req,<EOL>'<STR_LIT>': template['<STR_LIT:id>'],<EOL>'<STR_LIT>': num_created})<EOL><DEDENT><DEDENT>elif available >= desired + capacity:<EOL><INDENT>num_req = int(<EOL>math.floor((available - desired) / (<NUM_LIT:1.0> * capacity)))<EOL>num_deleted = self._delete_idle_service_vm_hosting_devices(<EOL>context, num_req, template)<EOL>if num_deleted < num_req:<EOL><INDENT>LOG.warning('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>{'<STR_LIT>': num_req, '<STR_LIT>': template['<STR_LIT:id>'],<EOL>'<STR_LIT>': num_deleted})<EOL><DEDENT><DEDENT><DEDENT>finally:<EOL><INDENT>lock.release()<EOL><DEDENT>
Maintains the pool of hosting devices that are based on <template>.

        Ensures that the number of standby hosting devices (essentially
        service VMs) is kept at a suitable level so that resource creation is
        not slowed down by booting of the hosting device.

        :param context: context for this operation
        :param template: db object for hosting device template