【发布时间】:2017-10-29 16:37:44
【问题描述】:
你好,我想在我的 Django 项目中使用 celery 任务,我尝试遵循来自 Mr.Vitor Freitas 的这个非常好的 tutorial。
但在我的情况下,如果尝试运行该教程 project 我没有得到结果,函数不会执行,在我的情况下和教程中(我接受消息等待并刷新,刷新后什么也没有)。
知道为什么吗?
我认为问题可能出在 RABBITQM 服务器中?一些配置?
只需安装 Erlang(otp_win64_20.1.exe) 和 RabbitMQ(rabbitmq-server-3.6.12.exe) 之后
这里是示例代码:
settings.py
CELERY_BROKER_URL = 'amqp://localhost'
芹菜.py
from __future__ import absolute_import
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
app = Celery('mysite')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
tasks.py
import string
from django.contrib.auth.models import User
from django.utils.crypto import get_random_string
from celery import shared_task
@shared_task
def create_random_user_accounts(total):
for i in range(total):
username = 'user_{}'.format(get_random_string(10, string.ascii_letters))
email = '{}@example.com'.format(username)
password = get_random_string(50)
User.objects.create_user(username=username, email=email, password=password)
return '{} random users created with success!'.format(total)
_ _init_ _.py
from .celery import app as celery_app
__all__ = ['celery_app']
views.py
from django.contrib.auth.models import User
from django.contrib import messages
from django.views.generic import TemplateView
from django.views.generic.list import ListView
from django.views.generic.edit import FormView
from django.shortcuts import redirect
from .forms import GenerateRandomUserForm
from .tasks import create_random_user_accounts
class UsersListView(ListView):
template_name = 'core/users_list.html'
model = User
class GenerateRandomUserView(FormView):
template_name = 'core/generate_random_users.html'
form_class = GenerateRandomUserForm
def form_valid(self, form):
total = form.cleaned_data.get('total')
create_random_user_accounts.delay(total)
messages.success(self.request, 'We are generating your random users! Wait a moment and refresh this page.')
return redirect('users_list')
cd 我的项目路径后的详细信息> celery -A mysite worker -l info:
C:\Windows\System32>cd C:\Users\username\Desktop\django-celery-example-master
C:\Users\username\Desktop\django-celery-example-master>celery -A mysite worker -l info
-------------- celery@pc name v4.1.0 (latentcall)
---- **** -----
--- * *** * -- Windows-8-6.2.9200 2017-10-29 18:10:24
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: mysite:0x404e5c0
- ** ---------- .> transport: amqp://guest:**@localhost:5672//
- ** ---------- .> results: disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery
[tasks]
. mysite.core.tasks.create_random_user_accounts
[2017-10-29 18:10:24,596: CRITICAL/MainProcess] Unrecoverable error: TypeError('must be integer<K>, not _subprocess_handle',)
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\celery\worker\worker.py", line 203, in start
self.blueprint.start(self)
File "C:\Python27\lib\site-packages\celery\bootsteps.py", line 119, in start
step.start(parent)
File "C:\Python27\lib\site-packages\celery\bootsteps.py", line 370, in start
return self.obj.start()
File "C:\Python27\lib\site-packages\celery\concurrency\base.py", line 131, in start
self.on_start()
File "C:\Python27\lib\site-packages\celery\concurrency\prefork.py", line 112, in on_start
**self.options)
File "C:\Python27\lib\site-packages\billiard\pool.py", line 1007, in __init__
self._create_worker_process(i)
File "C:\Python27\lib\site-packages\billiard\pool.py", line 1116, in _create_worker_process
w.start()
File "C:\Python27\lib\site-packages\billiard\process.py", line 124, in start
self._popen = self._Popen(self)
File "C:\Python27\lib\site-packages\billiard\context.py", line 383, in _Popen
return Popen(process_obj)
File "C:\Python27\lib\site-packages\billiard\popen_spawn_win32.py", line 64, in __init__
_winapi.CloseHandle(ht)
TypeError: must be integer<K>, not _subprocess_handle
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Python27\lib\site-packages\billiard\spawn.py", line 159, in spawn_main
new_handle = steal_handle(parent_pid, pipe_handle)
File "C:\Python27\lib\site-packages\billiard\reduction.py", line 126, in steal_handle
_winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE)
WindowsError: [Error 6]
【问题讨论】:
标签: python django rabbitmq celery