对于python来讲,RQ相对比于celery来说简单很多,配置容易,逻辑简单,实现速度快。但是,这个框架只适合Linux和unix系统。下面叙述一下django项目配置RQ步骤。
Requirements
Installation
Install django-rq (or download from PyPI):
pip install django-rq
Add django_rq to INSTALLED_APPS in settings.py:
INSTALLED_APPS = (
# other apps
“django_rq”,
)
Configure your queues in django’s settings.py (syntax based on
Django’s database config):
RQ_QUEUES = {
‘default’: {
‘HOST’: ‘localhost’,
‘PORT’: 6379,
‘DB’: 0,
‘PASSWORD’: ‘some-password’,
‘DEFAULT_TIMEOUT’: 360,
}
Include django_rq.urls in your urls.py:
# For Django < 2.0
urlpatterns += [
url(r’^django-rq/’, include(‘django_rq.urls’)),
]
# For Django >= 2.0
urlpatterns += [
path(‘django-rq/’, include(‘django_rq.urls’))
]
@job decorator
To easily turn a callable into an RQ task, you can also use the @job decorator that comes with django_rq:
from django_rq import job
@job
def long_running_func():
pass
long_running_func.delay() # Enqueue function in “default” queue
@job(‘high’)
def long_running_func():
pass
long_running_func.delay() # Enqueue function in “high” queue
Running workers
django_rq provides a management command that starts a worker for every queue specified as arguments:
python manage.py rqworker high default low