Google App Engine - Python - Task Queue - How can i add a list of tasks?

Viewed 315

I have this code that works fine:

taskqueue.add(url = MY_URL, params={'id': 42}, queue_name='random-message')

In this official document it says: "Adds a task or list of tasks into this queue."

But I can't understand how.

I already tried this:

tasks = []
tasks.append(taskqueue.Task(url = MY_URL, params={'id': 42}))
taskqueue.add(tasks, queue_name='random-message')

but It raises an error that I don't understand:

'Task payloads must be strings; invalid payload: %r' % payload)

I tried many others little variants that weren't working anyway.

2 Answers

The problem was:

taskqueue.add(task)

It cannot receive more than one task at a time. The right way to do that is this:

taskqueue.Queue.add(tasks)

My code is now working:

tasks = []
tasks.append(taskqueue.Task(url = MY_URL, params={'id': 42}))
taskqueue.Queue('random-message').add(tasks)

You might be having a naming conflict, since tasks is a parameter for the .add() method. Try:

task_list = []
task_list.append(taskqueue.Task(url = MY_URL, params={'id': 42}))
taskqueue.add(tasks=task_list, queue_name='random-message')

or:

taskqueue.add(task_list, queue_name='random-message')
Related