项目作者: kai3341

项目描述 :
Add taskcls decorator without waiting celery 4.5 and upgrade
高级语言: Python
项目地址: git://github.com/kai3341/celery-decorator-taskcls.git
创建时间: 2019-09-28T20:55:04Z
项目社区:https://github.com/kai3341/celery-decorator-taskcls

开源协议:MIT License

下载


Celery Decorator taskcls

python
version
downloads
format

  • Free software: MIT License

Logo

This package is temporary way to get app.taskcls decorator right now.
The main target of this package make you able to use taskcls decorator
before celery 4.5 released, and then you can remove this package import
without application code change

More about: https://github.com/celery/celery/pull/5755

Features

  1. import celery_decorator_taskcls
  2. celery_decorator_taskcls.patch_celery()
  3. from celery import Celery
  4. app = Celery(...)
  5. class BaseTask:
  6. def __init__(self, task, **kwargs):
  7. self.task = task
  8. for key, value in kwargs.items():
  9. setattr(self, key, value)
  10. @classmethod
  11. def task(cls, task, **kwargs):
  12. instance = cls(task, **kwargs)
  13. return instance.main()
  14. @app.taskcls(bind=True)
  15. class SimpleTask(BaseTask):
  16. def main(self):
  17. ...

app.taskcls decorator behavior is the same as app.task. You can pass it
kwargs like bind, name or other or you can use it without kwargs

You can also pass default decorator options by nested class MetaTask:

  1. class BaseTask:
  2. class MetaTask:
  3. bind = True
  4. @classmethod
  5. def task(cls, taks, *args, **kwargs):
  6. ...

Patching options

By default patcher search Celery.taskcls attribute. If it not found, patcher
creates it. But when it exists (I belive you find it in Celery 4.5), patcher
checks its optional argument force, because it seems patching not required.
Calling celery_decorator_taskcls.patch_celery(force=True) enforces
patching Celery even Celery.taskcls exists