Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk Enqueue Google Cloud Tasks

As part of migrating my Google App Engine Standard project from python2 to python3, it looks like I also need to switch from using the Taskqueue API & Library to google-cloud-tasks.

In the taskqueue library I could enqueue upto 100 tasks at a time like this

taskqueue.Queue('default').add([...task objects...])

as well as enqueue tasks asynchronously.

In the new library as well as the new API, it looks like you can only enqueue tasks one at a time

  • https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks/create
  • https://googleapis.dev/python/cloudtasks/latest/gapic/v2/api.html#google.cloud.tasks_v2.CloudTasksClient.create_task

I have an endpoint where it receives a batch with thousands of elements, each of which need to get processed in an individual task. How should I go about this?

like image 646
Alex Avatar asked May 15 '26 13:05

Alex


1 Answers

According to the official documentation (reference 1, reference 2) the feature of adding task to queues asynchronously (as this post suggests for adding bulk number of tasks to a queue), is NOT an available feature via Cloud Tasks API. It is available for the users of App Engine SDK though.

However, there is a reference in the documentation regarding adding a large number of Cloud Tasks to a queue via double-injection pattern workaround (this post might seem useful too).

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.

Since you are in a migration process, I figured out that this link would be useful, as it concerns migrating from Task Queue to Cloud Tasks (as you stated you are thinking to do).

Additional information on migrating your code with all the available details you can find here and here, regarding Pull queues to Cloud Pub/Sub Migration and Push queues to Cloud Tasks Migration correspondingly.

like image 176
Artemis Georgakopoulou Avatar answered May 17 '26 07:05

Artemis Georgakopoulou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!