def _queue_task(self, task_key, args={}, priority=5): """ Adds a (root) task that is to be run. Under the hood, the scheduler creates a task instance for the task, puts it into the queue, and then tries to advance the queue. """ logger.info('Queued Task: %s - Args: %s' % (task_key, args)) task_instance = TaskInstance() task_instance.task_key = task_key task_instance.priority = priority task_instance.args = simplejson.dumps(args) task_instance.queued = datetime.now() task_instance.status = STATUS_STOPPED task_instance.save() # queue the root task as the first work request. This lets the queue # advancement logic to function the same for a root task or a subtask task_instance.queue_worker_request(task_instance) with self._queue_lock: heappush(self._queue, [task_instance.compute_score(),task_instance]) # cache this task self._active_tasks[task_instance.id] = task_instance threads.deferToThread(self._schedule) return task_instance