如何解决芹菜文件中关键字参数namespace =“'CELERY”错误的芹菜工人配置

时间:2019-02-22 19:53:55

标签: python django pycharm celery-task

我有一个名为ShippingApp的项目,并且按照步骤设置了芹菜工人。我将celery 3.1.26.post2与python3.7配合使用,当我想启动Celery Worker时,出现以下错误:

E:\ShippingApp>celery -A ShippingApp worker -l info
Traceback (most recent call last):
  File "c:\program files\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\program files\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Program Files\Python37\Scripts\celery.exe\__main__.py", line 9, in <module>
  File "c:\program files\python37\lib\site-packages\celery\__main__.py", line 30, in main
    main()
  File "c:\program files\python37\lib\site-packages\celery\bin\celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "c:\program files\python37\lib\site-packages\celery\bin\celery.py", line 793, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "c:\program files\python37\lib\site-packages\celery\bin\base.py", line 309, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "c:\program files\python37\lib\site-packages\celery\bin\base.py", line 469, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "c:\program files\python37\lib\site-packages\celery\bin\base.py", line 489, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "c:\program files\python37\lib\site-packages\celery\app\utils.py", line 235, in find_app
    sym = symbol_by_name(app, imp=imp)
  File "c:\program files\python37\lib\site-packages\celery\bin\base.py", line 492, in symbol_by_name
    return symbol_by_name(name, imp=imp)
  File "c:\program files\python37\lib\site-packages\kombu\utils\__init__.py", line 96, in symbol_by_name
    module = imp(module_name, package=package, **kwargs)
  File "c:\program files\python37\lib\site-packages\celery\utils\imports.py", line 101, in import_from_cwd
    return imp(module, package=package)
  File "c:\program files\python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "E:\ShippingApp\ShippingApp\__init__.py", line 2, in <module>
    from .celery import app as celery_app
  File "E:\ShippingApp\ShippingApp\celery.py", line 6, in <module>
    app.config_from_object('django.conf:settings', namespace='CELERY')
TypeError: config_from_object() got an unexpected keyword argument 'namespace'

celery.py:

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()

初始化 .py:

from .celery import app as celery_app

tasks.py:

from celery import task
from django.core.mail import send_mail
from .models import Order
@task
def order_created(order_id):
    """
    Task to send an e-mail notification when an order is
    successfully created.
    """
    order = Order.objects.get(id=order_id)
    subject = 'Order nr. {}'.format(order.id)
    message = 'Dear {},\n\nYou have successfully placed an order.\
    Your order id is {}.'.format(order.first_name,
    order.id)
    mail_sent = send_mail(subject,
                        message,
                        'admin@myshop.com',
    [order.email])
    return mail_sent

在订单视图中,我们有以下代码:

def order_create(request):
cart = Cart(request)
if request.method == 'POST':
    form = OrderCreateForm(request.POST)
    if form.is_valid():
        order = form.save()
        for item in cart:
            OrderItem.objects.create(order=order,
                                    product=item['product'],
                                    price=item['price'],
                                    quantity=item['quantity'])
        # clear the cart
        cart.clear()
        # launch asynchronous task
        order_created.delay(order.id)
        # set the order in the session
        request.session['order_id'] = order.id
        # redirect for payment
        return redirect(reverse('payment:process'))
else:
    form = OrderCreateForm()
return render(request,
            'orders/order/create.html',
            {'cart': cart, 'form': form})

在我使用celery 4.2.1之前,但它与Windows 10不兼容,因此我将其卸载并安装版本3.1.26.post2。 请协助。

1 个答案:

答案 0 :(得分:0)

大写名称空间意味着所有celery配置必须以大写而不是小写指定,并以 CELERY _ 开头,因此,例如, task_always_eager 设置将变为 CELERY_TASK_ALWAYS_EAGER ,然后 broker_url 变为 CELERY_BROKER_URL 等等。此配置是从celery4.0开始引入的。

因此,对于版本<4,您无需在行中插入template<class T> class Command { public: typedef void (T::*Action)(); Command( T* object, Action method ) { m_object = object; m_method = method; } void execute() { (m_object->*m_method)(); } private: T* m_object; Action m_method; }; int main( void ) { Queue<Command<test>> que; Command<test>* input[] = { new Command<test>( new test, &test::m1), new Command<test>( new test, &test::m2), new Command<test>( new test, &test::m3)}; for (int i=0; i < 3; i++) que.enque( input[i] ); for (int i=0; i < 3; i++) que.deque()->execute(); cout << '\n'; }

library(datasets) data(cars) cars <- as.data.table(cars) apply(cars,2,table)

将以上内容替换为:

library(datasets) data(cars) cars <- as.data.table(cars) for (i in names(cars)){ print(table(cars[,(i) := as.character(get(i))])) }

注意::如果您使用的是celery 3.1,请检查您的Django版本。应该是<1.8