项目作者: datadvance

项目描述 :
Job manager for Django Channels
高级语言: Python
项目地址: git://github.com/datadvance/DjangoChannelsJobmanager.git
创建时间: 2018-06-27T11:57:09Z
项目社区:https://github.com/datadvance/DjangoChannelsJobmanager

开源协议:MIT License

下载


Job manager for Django Channels

Features

  • Offload long-running functions (jobs) to Channels workers.
  • Job may report the progress.
  • Job can be canceled.
  • Job can be implemented as sync or async function.

Quick start

  1. Add channels_jobmanager to your INSTALLED_APPS setting:
  1. INSTALLED_APPS = [
  2. ...
  3. 'channels_jobmanager',
  4. ]
  1. Subclass JobManager:
  1. class MyJobs(channels_jobmanager.JobManager):
  2. """Example job manager class."""
  3. # Allow anonymous user to submit jobs.
  4. allow_anonymous_user = True
  5. # Watch over job state changes.
  6. @classmethod
  7. def on_job_update(cls, job_info):
  8. """Here we receive job progress messages."""
  9. print('JOB STATE CHANGES:', job_info)
  1. Create simple job function and decorate it with job() :
  1. @MyJobs.job()
  2. async def my_job(count):
  3. """Simple counter job."""
  4. for i in range(count):
  5. # Simulate doing the job with sleep.
  6. await asyncio.sleep(delay=1)
  7. # Report progress.
  8. yield {
  9. 'message': 'Making a good progress...',
  10. 'payload': {'step index': i},
  11. 'readiness': (i + 1) / count
  12. }
  13. # Report final result.
  14. yield {
  15. 'message': 'Job well done!',
  16. 'payload': {'steps made': count},
  17. 'readiness': 1.0
  18. }
  1. Submit the job by invoking the decorated job function:
  1. job_info = my_job(message='Hi!')
  1. Cancel the job:
  1. my_job.cancel(job_id=job_info.job_id)
  1. Create database models, run Django and Channels worker.
  1. python manage.py migrate
  2. python manage.py runserver
  3. python manage.py runworker myjobs

Contributing

This project is developed and maintained by DATADVANCE LLC. Please
submit an issue if you have any questions or want to suggest an
improvement.

Acknowledgements

This work is supported by the Russian Foundation for Basic Research
(project No. 15-29-07043).