Skip to main content

Celery pool to run coroutine tasks

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

Celery Pool AsyncIO

python version downloads format

Logo

  • Free software: Apache Software License 2.0

Features

import asyncio
from celery import Celery

# celery_pool_asyncio importing is optional
# It imports when you run worker or beat if you define pool or scheduler
# but it does not imports when you open REPL or when you run web application.
# If you want to apply monkey patches anyway to make identical environment
# when you use REPL or run web application app it's good idea to import
# celery_pool_asyncio module
import celery_pool_asyncio  # noqa
# Sometimes noqa does not disable linter (Spyder IDE)
celery_pool_asyncio.__package__


app = Celery()


@app.task(
    bind=True,
    soft_time_limit=42,  # raises celery.exceptions.SoftTimeLimitExceeded inside the coroutine
    time_limit=300,  # breaks coroutine execution
)
async def my_task(self, *args, **kwargs):
    await asyncio.sleep(5)


@app.task
async def my_simple_task(*args, **kwargs):
    await asyncio.sleep(5)

Then run celery:

$ celery worker -A hello_async_celery.app -P celery_pool_asyncio:TaskPool

Monkey patching: wtf and why

There are many monkey patches should be applied to make application working, and some of them should be applied as early as possible. You are able to switch off any of them by setting environment variable CPA_MONKEY_DENY. Remember you should have a great reason to do it.

Except critical for work features it allows:

# await data sending to broker
async_result = await my_simple_task.delay()

# await wainting for AsyncResult
result = await async_result.get()

You can manually disable any of them by enumerating it comma separated:

$ env CPA_MONKEY_DENY=CELERY.SEND_TASK,ALL_BACKENDS celery worker -A hello_async_celery.app -P celery_pool_asyncio:TaskPool

Disabling is available for:

  • SIGNAL.SEND
  • CELERY.SEND_TASK
  • WORKCONTROLLER.USE_EVENTLOOP
  • WORKER.CPU_COUNT
  • BASERESULTCONSUMER.WAIT_FOR_PENDING
  • BASERESULTCONSUMER.DRAIN_EVENTS_UNTIL
  • ASYNCBACKENDMIXIN.WAIT_FOR_PENDING
  • ALL_BACKENDS
  • BEAT.SERVICE.START
  • BEAT.SERVICE.STOP
  • BUILD_TRACER
  • KOMBU.UTILS.COMPAT
  • RPC.RESULTCONSUMER.DRAIN_EVENTS
  • AMQPBACKEND.DRAIN_EVENTS
  • AMQPBACKEND.GET_MANY
  • AMQP_BACKEND
  • RPC_BACKEND

Scheduling

Default scheduler doesn't work. PersistentScheduler is subclass of default celery scheduler.

Running celery with scheduler:

$ celery worker -A hello_async_celery.app -P celery_pool_asyncio:TaskPool --scheduler celery_pool_asyncio:PersistentScheduler
$ celery beat -A hello_async_celery.app --scheduler celery_pool_asyncio:PersistentScheduler

Embeding also supported:

$ celery worker -A hello_async_celery.app -P celery_pool_asyncio:TaskPool --scheduler celery_pool_asyncio:PersistentScheduler -B

WARNING: embeded scheduler startup is not stable. It starts correctly in ~50% of cases. It looks like race condition. But after correct startup it works well. That's why it's good idea to run scheduler in separated process.

Celery Signals

from celery.signals import worker_init, worker_shutting_down


@worker_init.connect
async def do_startup_async(sender, **kwargs):
    # Coroutine functions are available after pool initialized
    await MyClass.init_async()


@worker_init.connect
def do_startup(sender, **kwargs):
    # regular functions are available too
    pass


@worker_shutting_down.connect
async def do_shutdown(sender=None, **kwargs):
    await MyClass.shutdown()

More examples

There is an example project uses celery-pool-asyncio.

Changelog

[0.2.0]

  • Allow to decorate corofunctions by celery.signals
  • Implement pool capacity (-c, --concurency)

[0.1.12]

  • Finalize monkey patcher refactoring. Now you able to switch off applying of any monkey patch. Remember with great power comes great responsibility
  • Implement soft_time_limit
  • Implement revoke
  • Fix keywords

[0.1.11]

  • Total monkey patching refactor. Now it is enabled by default, but you can manually disable some of features using environment variable CPA_MONKEY_DENY

[0.1.10]

  • Make Celery Beat working
    • Add async Celery Scheduler
    • More monkey patching
  • Move loop and loop_runner to own module
    • Avoid creating multiple loops and loop_runners per application

[0.1.9]

  • Large rework of await AsyncResult.get()
    • Works much better than earlier, but it's crap still
    • Added outnumber of monkey-patches
  • Fixed race condition on first microseconds of pool shutdown

[0.1.8]

  • Cleanup tracer, use celery.app.trace namespase where it possible

[0.1.7]

  • Refactor monkey, split it
  • Move patch_send_task to own function
  • Add patch_result_get to await AsyncResult.get

[0.1.6]

  • Avoid building trace twice
  • Also this small performance optimization fixed AsyncResult.get

[0.1.5]

  • Fix graceful shutdown

[0.1.4]

  • Fix monkey: another function must be patched

[0.1.3]

  • Add changelog
  • Append documentation

[0.1.2]

  • Add monkey patcher to make brocker IO operations nonblocking

[0.1.1]

  • Refactor code
  • Fix found errors

[0.1.0]

  • Initial commit

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

celery-pool-asyncio-0.2.0.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

celery_pool_asyncio-0.2.0-py2.py3-none-any.whl (19.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file celery-pool-asyncio-0.2.0.tar.gz.

File metadata

  • Download URL: celery-pool-asyncio-0.2.0.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for celery-pool-asyncio-0.2.0.tar.gz
Algorithm Hash digest
SHA256 330e0ff31a7f6e3ea9cb004843f2f967601602b52cee05a2faea7ed65b76a5b8
MD5 0477930494cf492c691d94f577821115
BLAKE2b-256 7f078005ac90afb30c43c22dd7a8f425c7c3b6785bdc484cb27662a35f6d87d1

See more details on using hashes here.

File details

Details for the file celery_pool_asyncio-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: celery_pool_asyncio-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for celery_pool_asyncio-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 72cdec065060bc99af57771cba7f0444d8a901842ee3c859d0d167c547764a21
MD5 96951d0ed1f796c627d26ff2e640b996
BLAKE2b-256 8e8be6e6535df47bbeae8387a8ed9512155fb98bd72550be1b599fef43636c28

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page