django芹菜延迟功能不要exetute

时间:2017-10-29 16:37:44

标签: python django rabbitmq celery

你好我想在我的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'

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:\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]

1 个答案:

答案 0 :(得分:2)

Celery 4.x does not support Windows,根据我的经验,这是打破的prefork /台球部分。使用--pool solo运行工作程序应该可以正常工作。