Skip to main content

Async Cloud Tasks

Use Cloud Tasks to easily defer functions executions in your web applications 🍯

Inspired by django-cloud-tasks, thanks to GeorgeLubaretsi 👏🏻👏🏻👏🏻

Integrate Google Cloud Tasks with web applications without the need to create an exclusive endpoint to handle tasks. The package provides easy to use decorator to create task handlers. App looks for tasks in cloud_tasks.py files in your installed applications and auto-registers them.

Prerequisites

  • Python 3.7+

Dependencies

Support

  • IMPORTANT: Now it is only giving support for Django framework.

Installation

Install the package from PyPi

pip install async-cloud-tasks

Add async_cloud_tasks to INSTALLED_APPS:

INSTALLED_APPS = (
    # ...
    'async_cloud_tasks',
    # ...
)

Add configuration to your settings

ASYNC_CLOUD_TASKS={
    'gcp_location': '<GCP TASK QUEUE LOCATION>',
    'gcp_project_name': '<GCP TASK QUEUE PROJECT NAME>',
    'task_handler_root_url': '_tasks/',
    'service_url': '<BASE SERVICE URL>',
    'handler_secret': '<SECRET KEY TO BE USED FOR TASK HANDLER AUTHENTICATION>'
}

# This setting allows you to debug your cloud tasks by running actual task handler function locally
# instead of sending them to the task queue. Default: False
ASYNC_CLOUD_TASKS_EXECUTE_LOCALLY = False

Add cloud task views to your urls.py (must resolve to the same url as task_handler_root_url)

# urls.py
# ...
from django.urls import path, include
from async_cloud_tasks import urls as dct_urls

urlpatterns = [
    # ...
    path('_tasks/', include(dct_urls)),
]

Quick start (only for Django)

Simply import the task decorator and define the task inside cloud_tasks.py in your app. First parameter should always be request which is populated after task is executed by Cloud Task service.

You can get actual request coming from Cloud Task service by accessing request.request in your task body and additional attributes such as: request.task_id, request.request_headers

# cloud_tasks.py
# ...
from async_cloud_tasks.decorators import task

@task(queue='default')
def example_task(request, p1, p2):
    print(p1, p2)
    print(request.task_id)

Pushing the task to the queue:

from my_app.cloud_tasks import example_task

example_task(p1='1', p2='2').defer()

It is also possible to run an actual function using run method of CloudTaskWrapper object instance that is returned after task is called (this can be useful for debugging):

task = example_task(p1=i, p2=i)
task.run()

References

Metadata

Release files for async-cloud-tasks 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async-cloud-tasks 0.0.3
File Size Uploaded
async-cloud-tasks-0.0.3.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for async-cloud-tasks 0.0.3
File Interpreter ABI Platform
async_cloud_tasks-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 21.6 kB

Release files / async-cloud-tasks-0.0.3.tar.gz

Download URL async-cloud-tasks-0.0.3.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e3770e8ba32eddc89efb4afc80a1542bde46209309b390ebd4a9aba55be68225
BLAKE2b-256 checksum
How to use checksums
71110d00a4b71af0cc8963993f72021636a92fee9364ca06dd59afcab2cc2681
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.9

Release files / async_cloud_tasks-0.0.3-py3-none-any.whl

Download URL async_cloud_tasks-0.0.3-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7152a91e3d115ee517dc90df86f144697b2142df0ad33dc1a8e1b173915931d6
BLAKE2b-256 checksum
How to use checksums
6cf350930eea76c6aa423b365ce285540ad5e98b9134212e19893bf8d36de16d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.9

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page