docs: def get_port_for_process(self, pid):
          """Allocates and returns port for pid or 0 if none could be allocated."""
          if not self._port_queue:
              raise RuntimeError('No ports being managed.')
  
          # Avoid an infinite loop if all ports are currently assigned.
          check_count = 0
          max_ports_to_test = len(self._port_queue)
          while check_count < max_ports_to_test:
              # Get the next candidate port and move it to the back of the queue.
              candidate = self._port_queue.pop()
              self._port_queue.appendleft(candidate)
              check_count += 1
              if (candidate.start_time == 0 or
                  candidate.start_time != _get_process_start_time(candidate.pid)):
                  if _is_port_free(candidate.port):
                      candidate.pid = pid
                      candidate.start_time = _get_process_start_time(pid)
                      if not candidate.start_time:
                          log.info("Can't read start time for pid %d.", pid)
                      self.ports_checked_for_last_request = check_count
                      return candidate.port
                  else:
                      log.info(
                          'Port %d unexpectedly in use, last owning pid %d.',
                          candidate.port, candidate.pid)
  
          log.info('All ports in use.')
          self.ports_checked_for_last_request = check_count
          return 0