Python Celery 和 Django 的简单例子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20164688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Celery and Django simple example
提问by Teodor Scorpan
Let's take a simple Django example.
让我们举一个简单的 Django 例子。
app/models.py
应用程序/模型.py
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
token = models.CharField(max_length=32)
app/views.py
应用程序/视图.py
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from forms import RegisterForm
from utils.utilities import create_user
@csrf_exempt
def register_view(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
create_user(form.cleaned_data)
return HttpResponse('success')
utils/utilities.py
实用程序/实用程序.py
def create_user(data):
user = User.objects.create_user(username=data['username'], email=None, password=data['password'])
user.save()
profile = UserProfile()
profile.user = user
profile.token = generate_token()
profile.save()
Can somebody provide an implementation of Celery in this example? Imagine this is a large project with hundreds of requests per sec.
有人可以在此示例中提供 Celery 的实现吗?想象一下,这是一个每秒有数百个请求的大型项目。
采纳答案by Joseph Victor Zammit
Assuming you have both Python's celeryand django-celery installed, create the following tasks.pyfile under your app:
假设您同时安装了 Python 的celery和django-celery,tasks.py在您的应用程序下创建以下文件:
utils/tasks.py
实用程序/任务.py
from celery import task
# other imports
@task()
def create_user(data):
user = User.objects.create_user(
username=data['username'], email=None, password=data['password']
)
user.save()
profile = UserProfile()
profile.user = user
profile.token = generate_token()
profile.save()
return None
Delete your utils/utilities.pyfile in your example above.
删除utils/utilities.py上面示例中的文件。
In your code in views.pychange the create_usercall from:
在您的代码中views.py更改create_user调用:
create_user(form.cleaned_data)
to:
到:
create_user.delay(form.cleaned_data)
Basically create_useris now a celery task; if you have the right Python packages installed (as mentioned above), code-wise (the implementation you ask for) that's it. delayexecutes your function asynchronously - i.e. the HTTP response is returned without waiting for the asynchronous task to complete.
create_user现在基本上是celery任务;如果您安装了正确的 Python 包(如上所述),那么代码方面(您要求的实现)就是这样。delay异步执行您的函数 - 即,无需等待异步任务完成即可返回 HTTP 响应。
Locally you can run a celery daemon process using python manage.py celeryd.
在本地,您可以使用python manage.py celeryd.
In production you have to set up the celery process itself using for instance upstart, supervisoror any other tool to control the lifecycle of such process.
在生产中,你必须设置使用例如芹菜过程本身upstart,supervisor或任何其他工具来控制这种流程的生命周期。
Further details documented here.

