你好,我想在我的 Django 项目中使用芹菜任务,我尝试遵循 Vitor Freitas 先生的这个非常好的教程。
但是就我而言,如果尝试运行它,该教程项目我没有返回结果,函数不会执行,就我而言,在教程中(我接受消息等待并刷新,刷新后什么都没有)。
知道为什么吗?
我认为所以问题可能出在 RABBITQM 服务器 - 一些配置 ?
只需安装 Erlang( otp_win64_20.1.exe
) 并在 RabbitMQ( rabbitmq-server-3.6.12.exe
之后
这里示例代码:
settings.py
CELERY_BROKER_URL = 'amqp://localhost'
celery.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:WindowsSystem32>cd C:UsersusernameDesktopdjango-celery-example-master
C:UsersusernameDesktopdjango-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:Python27libsite-packagesceleryworkerworker.py", line 203, in start
self.blueprint.start(self)
File "C:Python27libsite-packagescelerybootsteps.py", line 119, in start
step.start(parent)
File "C:Python27libsite-packagescelerybootsteps.py", line 370, in start
return self.obj.start()
File "C:Python27libsite-packagesceleryconcurrencybase.py", line 131, in start
self.on_start()
File "C:Python27libsite-packagesceleryconcurrencyprefork.py", line 112, in on_start
**self.options)
File "C:Python27libsite-packagesbilliardpool.py", line 1007, in __init__
self._create_worker_process(i)
File "C:Python27libsite-packagesbilliardpool.py", line 1116, in _create_worker_process
w.start()
File "C:Python27libsite-packagesbilliardprocess.py", line 124, in start
self._popen = self._Popen(self)
File "C:Python27libsite-packagesbilliardcontext.py", line 383, in _Popen
return Popen(process_obj)
File "C:Python27libsite-packagesbilliardpopen_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:Python27libsite-packagesbilliardspawn.py", line 159, in spawn_main
new_handle = steal_handle(parent_pid, pipe_handle)
File "C:Python27libsite-packagesbilliardreduction.py", line 126, in steal_handle
_winapi.DUPLICATE_SAME_ACCESS | _winapi.DUPLICATE_CLOSE_SOURCE)
WindowsError: [Error 6]
Celery 4.x不支持Windows,根据我的经验,它是前叉/台球部分损坏的。使用--pool solo
运行辅助角色应该可以工作。