Skip to main content

Django swappable tasks

Introduction

Considering you have a django project running on different servers each with its own asynchronous tasks handler this project helps you have one way to process the tasks while converting to the respective handler in the background. Currently Google Cloud Tasks and Celery are supported.

Code Samples

To set up you will need to add the following to your settings.py file DEFAULT_ASYNC_TASKS_HANDLER = env('DEFAULT_ASYNC_TASKS_HANDLER') This can either be CELERY or GOOGLE_CLOUD_TASKS.

If you are using celery remember to set up other settings such as CELERY_BROKER_URL, CELERY_RESULT_BACKEND, CELERY_ACCEPT_CONTENT, CELERY_TASK_SERIALIZER, CELERY_RESULT_SERIALIZER, CELERY_TIMEZONE

For google cloud tasks remember to set up the following settings GOOGLE_CLOUD_PROJECT_ID, GOOGLE_CLOUD_TASKS_LOCATION_NAME, GOOGLE_APPLICATION_CREDENTIALS

Add the view to your projects urls.py file as below

from django.urls import path, include
urlpatterns = [
          ...
          path('tasks/', include('django_swappable_tasks.urls')),    
          ...    
]

Assume you have a task as below

from myproject.celery_app import app

@app.task
def my_blocking_task(name, age):
      import time
      import random
      sleep_seconds = random.randint(20, 45)
      time.sleep(sleep_seconds)
      print("Hello {}, your age is {}".format(name, age)

To process a task asynchronously do it as below

from django_swappable_tasks.utils import process_task_asynchronously
from my project.tasks import my_blocking_task
task_kwargs = {"name": "John Doe", "age" : 18}
process_task_asynchronously(my_blocking_task, "google_default_queue", task_args=[], task_kwargs= task_kwargs)

Installation

To install the package do pip install django_swappable_tasks You can also directly install from The GitHub Repository

Release files for django-swappable-tasks 0.1.16

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

Source distribution (sdist)

Source distribution for django-swappable-tasks 0.1.16
File Size Uploaded
django_swappable_tasks-0.1.16.tar.gz 4.9 kB Details

Release files / django_swappable_tasks-0.1.16.tar.gz

Download URL django_swappable_tasks-0.1.16.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
155e9d4fcde9a0536c391349a129d5132ef5f5cf87598e3cc808fa83c8447fab
BLAKE2b-256 checksum
How to use checksums
1a72819d0ef7bea87ec61c1a2099a15a0dec26674115f5f479fb32c51b91d93d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.1.16 This release

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

1 release file

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