Google Cloud Tasks Python Client

raw JSON →
2.22.0 verified Tue May 12 auth: no python install: verified quickstart: verified

Cloud Tasks is a fully managed service for dispatching and delivering a large number of distributed tasks. The `google-cloud-tasks` Python client library, currently at version 2.22.0, provides an idiomatic way to interact with the Cloud Tasks API for creating, managing, and scheduling tasks. Google Cloud client libraries typically receive updates on an as-needed basis, often following changes in the underlying Google Cloud APIs.

pip install google-cloud-tasks
error google.api_core.exceptions.PermissionDenied: 403 The principal does not have cloudtasks.tasks.create permission for the resource queue.
cause The service account or user making the API call lacks the necessary IAM permissions to create tasks in the specified Cloud Tasks queue.
fix
Grant the Cloud Tasks Enqueuer role (roles/cloudtasks.enqueuer) to the service account or user. For HTTP target tasks, ensure the service account also has appengine.applications.get if it's an App Engine Flex or Standard environment.
error google.api_core.exceptions.InvalidArgument: 400 Request contains an invalid argument.
cause The task configuration provided to `create_task` is malformed or contains invalid values, such as a `schedule_time` in the past, an incorrectly formatted service account email in an OIDC token, or an invalid HTTP request body.
fix
Carefully review the task and http_request configuration. Ensure schedule_time is set for a future timestamp, the body for HTTP targets is correctly encoded (e.g., base64 for bytes), and all other parameters conform to the Cloud Tasks API's requirements for the chosen task type.
error ModuleNotFoundError: No module named 'google.cloud.tasks'
cause The `google-cloud-tasks` Python client library is not installed in the active Python environment, or there is an issue with the Python path. An older, deprecated `google-cloud` meta-package might also cause import conflicts.
fix
Install the specific google-cloud-tasks package using pip install google-cloud-tasks. If the generic google-cloud package is installed, uninstall it first (pip uninstall google-cloud) as it is deprecated and can interfere with specific client libraries. Ensure you are installing into the correct virtual environment.
error grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.UNAVAILABLE
cause This error typically indicates a transient issue with the Cloud Tasks service itself, network connectivity problems, or the target endpoint for the task is unresponsive or overloaded, leading to a timeout or service unavailability.
fix
Implement client-side retry logic using google.api_core.retry.Retry with a suitable predicate (e.g., if_transient_error) and appropriate exponential backoff delays. Check the Google Cloud status dashboard for any ongoing service outages. Also, ensure your task's target endpoint is healthy, accessible, and can process requests within the configured timeout.
gotcha Authentication is crucial. For local development, set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to a service account key file. For deployed applications, ensure the service account running your code has the `Cloud Tasks Enqueuer` role (or `cloudtasks.tasks.create` permission) for the queue, and if targeting authenticated endpoints (like Cloud Run), the task's OIDC token's service account must have permissions to invoke the target.
fix Set `GOOGLE_APPLICATION_CREDENTIALS` locally. Grant `roles/cloudtasks.enqueuer` to the caller for the queue, and appropriate invocation roles (e.g., `roles/run.invoker`) to the service account specified in `oidc_token.service_account_email` if using authenticated HTTP targets.
gotcha Cloud Tasks aims for exactly-once delivery but can occasionally execute tasks more than once due to system trade-offs. Your task handlers should be idempotent to safely handle duplicate executions without unintended side effects.
fix Design your task handlers to be idempotent. This means the outcome of processing a task multiple times is the same as processing it once. Techniques include using unique transaction IDs or checking state before applying changes.
gotcha Overwhelming your target service or improperly configuring queue rate limits can lead to backlogs and HTTP 429 (Too Many Requests) or 503 (Service Unavailable) errors. Cloud Tasks will implement backoff, but sustained issues require intervention.
fix Configure appropriate rate limits and concurrency settings on your Cloud Tasks queues. Ensure your target services (e.g., Cloud Run, App Engine) are scaled sufficiently to handle the expected task dispatch rate. Implement client-side rate limiting or exponential backoff when enqueueing tasks if you frequently hit `ResourceExhausted` errors.
gotcha When sending a payload with an HTTP task, the body must be explicitly encoded to bytes (e.g., `json.dumps(payload).encode('utf-8')`). Failing to encode will result in type errors.
fix Always encode the `body` field of an `HttpRequest` object to bytes using `.encode('utf-8')` (or similar) before sending.
breaking The client library requires Python 3.9 or newer. Older Python versions (3.8 and below) are unsupported.
fix Upgrade your Python environment to 3.9 or higher. For older Python versions, you might need to use an older version of the `google-cloud-tasks` library, but this is not recommended.
gotcha Default logging for `google-cloud-tasks` (and other `google` prefixed libraries) does not propagate to the root logger by default. If you expect to see logs via your application's root logger, you must explicitly enable propagation.
fix Add `import logging; logging.getLogger("google").propagate = True` to your application's initialization code if you want Google library logs to be handled by the root logger.
breaking Critical environment variables for GCP project, location, task queue, target URL, and service account email (`GCP_PROJECT_ID`, `GCP_LOCATION_ID`, `GCP_TASK_QUEUE_ID`, `TASK_TARGET_URL`, `TASK_SERVICE_ACCOUNT_EMAIL`) must be configured. The application cannot proceed without these settings.
fix Set all required environment variables: `GCP_PROJECT_ID`, `GCP_LOCATION_ID`, `GCP_TASK_QUEUE_ID`, `TASK_TARGET_URL`, and `TASK_SERVICE_ACCOUNT_EMAIL` with appropriate values for your Cloud Tasks setup.
breaking The Cloud Tasks client library, especially when used in examples or local development, requires specific environment variables to be set for project, location, queue, target URL, and service account email. Failing to set these will prevent basic operations or even application startup.
fix Ensure that `GCP_PROJECT_ID`, `GCP_LOCATION_ID`, `GCP_TASK_QUEUE_ID`, `TASK_TARGET_URL`, and `TASK_SERVICE_ACCOUNT_EMAIL` environment variables are properly defined and accessible by your application.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 1.66s 70.4M
3.10 slim (glibc) - - 1.01s 68M
3.11 alpine (musl) - - 2.42s 75.4M
3.11 slim (glibc) - - 1.57s 73M
3.12 alpine (musl) - - 2.56s 66.8M
3.12 slim (glibc) - - 1.99s 65M
3.13 alpine (musl) - - 2.43s 66.3M
3.13 slim (glibc) - - 1.93s 64M
3.9 alpine (musl) - - 1.50s 70.6M
3.9 slim (glibc) - - 1.18s 68M

This quickstart demonstrates how to create an HTTP task with OIDC authentication, suitable for targeting services like Cloud Run or Cloud Functions that require identity verification. It includes setting a payload and an optional schedule time. Ensure your Google Cloud environment is configured for Application Default Credentials (ADC) or explicitly set `GOOGLE_APPLICATION_CREDENTIALS` for local development.

import os
import json
import datetime
from google.cloud import tasks_v2
from google.cloud.tasks_v2.types import Task, HttpRequest, HttpMethod
from google.protobuf import timestamp_pb2

def create_http_task_with_auth(
    project_id: str,
    location_id: str,
    queue_id: str,
    url: str,
    service_account_email: str,
    payload: dict = None,
    schedule_time_seconds: int = None,
) -> Task:
    """Create an HTTP task with OIDC authentication.

    Args:
        project_id: The GCP project ID.
        location_id: The location of the queue (e.g., 'us-central1').
        queue_id: The ID of the Cloud Tasks queue.
        url: The full URL of the HTTP target (e.g., a Cloud Run service).
        service_account_email: The email of the service account for OIDC token.
        payload: (Optional) Dictionary payload to send with the task.
        schedule_time_seconds: (Optional) Delay in seconds before task execution.

    Returns:
        The created Task object.
    """
    client = tasks_v2.CloudTasksClient()

    # Construct the queue path.
    parent = client.queue_path(project_id, location_id, queue_id)

    # Construct the HTTP request for the task.
    http_request = HttpRequest(
        http_method=HttpMethod.POST,
        url=url,
        oidc_token={'service_account_email': service_account_email}
    )

    if payload:
        http_request.headers['Content-Type'] = 'application/json'
        http_request.body = json.dumps(payload).encode('utf-8')

    # Construct the task.
    task = Task(http_request=http_request)

    if schedule_time_seconds:
        # Schedule the task for a future time.
        future_time = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=schedule_time_seconds)
        timestamp = timestamp_pb2.Timestamp()
        timestamp.FromDatetime(future_time)
        task.schedule_time = timestamp

    # Send the task creation request.
    response = client.create_task(parent=parent, task=task)
    print(f"Created task: {response.name}")
    return response

# --- Example Usage ---
if __name__ == "__main__":
    # Set environment variables for authentication (e.g., GOOGLE_APPLICATION_CREDENTIALS)
    # and for the task details.
    project = os.environ.get('GCP_PROJECT_ID', 'your-gcp-project-id')
    location = os.environ.get('GCP_LOCATION_ID', 'us-central1')
    queue = os.environ.get('GCP_TASK_QUEUE_ID', 'my-http-queue')
    target_url = os.environ.get('TASK_TARGET_URL', 'https://your-cloud-run-service-url.run.app/task-handler')
    sa_email = os.environ.get('TASK_SERVICE_ACCOUNT_EMAIL', 'your-service-account@your-gcp-project-id.iam.gserviceaccount.com')

    if project == 'your-gcp-project-id':
        print("Please set GCP_PROJECT_ID, GCP_LOCATION_ID, GCP_TASK_QUEUE_ID, TASK_TARGET_URL, and TASK_SERVICE_ACCOUNT_EMAIL environment variables.")
    else:
        task_payload = {"message": "Hello from Cloud Tasks!", "source": "python-client"}
        try:
            created_task = create_http_task_with_auth(
                project,
                location,
                queue,
                target_url,
                sa_email,
                payload=task_payload,
                schedule_time_seconds=60 # Schedule 1 minute in the future
            )
            print(f"Successfully scheduled task: {created_task.name}")
        except Exception as e:
            print(f"Error creating task: {e}")
            print("Ensure Cloud Tasks API is enabled, queue exists, and authentication is set up.")