Skip to main content

problem

The Celery daemon needs to be restarted every time an existing task is modified or new tasks are added.

solution

Use only one Celery task that’s generic enough to run arbitrary Python functions with arbitrary arguments and wrap this task in a custom decorator.

As a bonus, the job will still work (synchronously) when Celery/the broker are not running.

The job’s calling API is the same as Celery’s: .s(), .delay() and .apply_async()

example

  • in celeryapp.py which is in the same directory as celeryconfig.py, create a Celery app and then import the custom decorator:

import celery
import os
import sys

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '.'))
#os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
app = celery.Celery()
app.config_from_object('celeryconfig')

from generic_celery_task.decorators import task
  • start Celery along with its broker. You no longer need to restart Celery after this.

  • in another file, importable from celeryapp.py, create your task:

from celeryapp import task

@task
def job(x, y):
    return 'x + y = %d' % (x + y)
  • now use it:

import celery

# direct function call
assert job(1, 2) == 'x + y = 3'

# using the delay() method
res = job.delay(1, 2)
# if the Celery daemon and its backend broker are running, 'res' is an instance of AsyncResult
if isinstance(res, celery.result.AsyncResult):
    assert res.get() == 'x + y = 3'
else:
    # if either one is not running, a warning is issued and the function is executed synchronously
    # with 'res' being the actual function result
    # if you want to silence the warnings, use '@task(quiet=True)'
    assert res == 'x + y = 3'

# using the apply_async() method
res = job.apply_async(args=[1, 2])
# and process the result as above, if you need to

installation

A setup.py is provided. You know what to do with it.

testing

The tests require nose, redis, redis-py and assume that the port 6389 is free.

Run the tests with “python setup.py test” or with “nosetests -v”.

This package was tested with python-2.7.6, python-3.3.4, nose-1.3.0, celery-3.1.10, redis-2.8.7 and redis-py-2.9.1 .

caveats

  • the module which holds your custom task will be reloaded. If it contains a class using ‘super’ and its instance, you might run into the problem described here. Apply one of the proposed fixes.

  • the state of the Celery daemon and its broker are checked only once, when the first .delay() or .apply_async() method is called on a custom task.

credits

Release files for generic_celery_task 0.2

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

Source distribution (sdist)

Source distribution for generic_celery_task 0.2
File Size Uploaded
generic_celery_task-0.2.tar.gz 5.6 kB Details

Release files / generic_celery_task-0.2.tar.gz

Download URL generic_celery_task-0.2.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
96f3530da8b3cf073ebdc17a92334e8e1bbbc3980deb9a6964ad10acb3e763d0
BLAKE2b-256 checksum
How to use checksums
b9911540e6cbabed30f51b580e4800a51ae4e4c91846d545f459e93ab9e21912
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.2 This release

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