1

I'm using App Engine to trigger the creation of multiple Cloud Tasks based on items in a list:

function_url="https://us-central1-myproject.cloudfunctions.net/some-cloud-function"

someTasks = [
{'id': 'task-1'},
{'id': 'task-2'},
{'id': 'task-3'},
...
{'id': 'task-1000'},
]

The tasks are currently created using:

Parallel(backend='threading', n_jobs=100)(
        delayed(create_document_task)(function_url=function_uri, data=task) for task in someTasks 
                    )

The above code creates the tasks in parallel and instructs the Task Queue to direct the payload to that specific cloud function.

Is doing this in parallel the correct way to rapidly create tasks?

3
  • 1
    Nope, you should do a batch submission to reduce the number of API calls. See stackoverflow.com/a/34457147/136598
    – minou
    Commented Nov 26, 2019 at 15:59
  • The old App Engine tasks queues had an async batch add like mentioned above. However, I do not recommend using the old functionality, especially if you have migrated over to Cloud Tasks. Commented Nov 26, 2019 at 16:10
  • @AveriKitsch is the above the old method? If not that way, do you know how to batch add tasks to a queue?
    – Ari
    Commented Nov 27, 2019 at 2:22

1 Answer 1

1
- I am posting this as an answer, due to the amount of text not fitting in a comment.

It seems that the aforementioned (in the comments) method:

  • Queue(“someQueue”).add_async(tasks)

is indeed an old method. This method is implemented within the Task Queue REST API (v1), in order to asynchronously add a task or list of tasks into a task queue.

However as stated here, The App Engine Task Queue REST API (v1), was turned down as of February 20, 2018. The separate product Cloud Tasks provides a REST API that you can use to add tasks from a second generation App Engine standard environment run-time, any of the App Engine flexible environment run-times, or even from entirely outside of App Engine.

This API does not include the “add_async()” functionality. More specifically, here and here is affirmed that the feature of adding task to queues asynchronously, as the users of App Engine SDK have the option to do, is NOT an available feature via Cloud Tasks API.

Nonetheless, when a large number of Cloud Tasks, for example millions or billions, need to be added, a double-injection pattern can be useful.

To implement this scenario, you'll need to create a new injector queue, whose single task would contain information to add multiple(100) tasks of the original queue that you're using. On the receiving end of this injector queue would be a service which does the actual addition of the intended tasks to your original queue. Although the addition of tasks in this service will be synchronous and 1-by-1, it will provide an asynchronous interface to your main application to bulk add tasks. In such a way you can overcome the limits of synchronous, 1-by-1 task addition in your main application.

Note that the 500/50/5 pattern of task addition to queue is a suggested method, in order to avoid any (queue/target) overloads.

As I did not find any examples of this implementation, I will edit the answer as soon as I find one.

I hope this helps.

3
  • Thanks this was really informative. In a round about way it seems like my original idea is heading in the right direction. But I should instead make it a service that can throttle and take in a batch of 'to-be-created' tasks from an original list/queue of tasks, but as a service it would also scale depending on the number of tasks in the original queue/list?
    – Ari
    Commented Nov 27, 2019 at 20:43
  • 1/2 - Yes. The service/injector queue can be sped up over time, for example start at 5 TPS, then increase by 50% every 5 minutes. Note that when scaling beyond 500 TPS, you should increase traffic by no more than 50% every 5 minutes. Following the 500/50/5 pattern of task addition in order to avoid queue/target overloads is the recommended practice. Commented Nov 28, 2019 at 10:33
  • 2/2 - Additionally, if the tasks are created by an App Engine app, you can leverage App Engine traffic splitting (Standard/Flex) to smooth traffic increases. This and this links could be useful for understanding the scenario. Commented Nov 28, 2019 at 10:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.