docs: def _insert_tasks(tasks, queue, transactional=False,
                    retry_transient_errors=True,
                    retry_delay=RETRY_SLEEP_SECS):
      """Insert a batch of tasks into the specified queue. If an error occurs
      during insertion, split the batch and retry until they are successfully
      inserted. Return the number of successfully inserted tasks.
      """
      from google.appengine.api import taskqueue
  
      if not tasks:
          return 0
  
      try:
          taskqueue.Queue(name=queue).add(tasks, transactional=transactional)
          return len(tasks)
      except (taskqueue.BadTaskStateError,
              taskqueue.TaskAlreadyExistsError,
              taskqueue.TombstonedTaskError):
          if len(tasks) <= 1:
              # Task has already been inserted, no reason to report an error here.
              return 0
  
          # If a list of more than one Tasks is given, a raised exception does
          # not guarantee that no tasks were added to the queue (unless
          # transactional is set to True). To determine which tasks were
          # successfully added when an exception is raised, check the
          # Task.was_enqueued property.
          reinsert = _tasks_to_reinsert(tasks, transactional)
          count = len(reinsert)
          inserted = len(tasks) - count
          inserted += _insert_tasks(reinsert[:count / 2], queue, transactional,
                                    retry_transient_errors, retry_delay)
          inserted += _insert_tasks(reinsert[count / 2:], queue, transactional,
                                    retry_transient_errors, retry_delay)
  
          return inserted
      except taskqueue.TransientError:
          # Always re-raise for transactional insert, or if specified by
          # options.
          if transactional or not retry_transient_errors:
              raise
  
          reinsert = _tasks_to_reinsert(tasks, transactional)
  
          # Retry with a delay, and then let any errors re-raise.
          time.sleep(retry_delay)
  
          taskqueue.Queue(name=queue).add(reinsert, transactional=transactional)
          return len(tasks)