将CELERY_ALWAYS_EAGER设置为True会引发错误



我的代码中有一些异步调用。像这样:

send_message_to_install.delay(install, alert.message, badge, code,
                            campaign=alert.campaign, **kwargs)

我对返回任何结果不感兴趣,重要的是在运行测试时同步执行函数,而不会引发错误。

测试时,我试图在设置文件中将CELERY_ALWAYS_EAGER设置为True:

if "test" in sys.argv:
    CELERY_ALWAYS_EAGER = True

我也试着在测试中设置它:

from django.conf import settings
settings.CELERY_ALWAYS_EAGER = True

这两次尝试都会使我的测试抛出错误(如果没有"延迟",它可以正常工作):

File "/opt/src/slicephone/cloud/users/api.py", line 520, in send_alert
campaign=alert.campaign, **kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/task.py", line 357, in delay
return self.apply_async(args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/task.py", line 463, in apply_async
options = router.route(options, self.name, args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/routes.py", line 44, in route
route = self.lookup_route(task, args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/app/routes.py", line 77, in lookup_route
return _first_route(self.routes, task, args, kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/utils/functional.py", line 196, in _matcher
answer = getattr(maybe_promise(obj), method)(*args, **kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/kombu/utils/functional.py", line 50, in maybe_promise
return value.evaluate()
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/utils/functional.py", line 162, in evaluate
self._value = super(mpromise, self).evaluate()
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/kombu/utils/functional.py", line 22, in evaluate
return self._fun(*self._args, **self._kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/celery/utils/imports.py", line 48, in instantiate
return symbol_by_name(name)(*args, **kwargs)
File "/opt/virtenvs/django_slice/local/lib/python2.7/site-packages/kombu/utils/__init__.py", line 80, in symbol_by_name
module = imp(module_name, package=package, **kwargs)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: No module named tasks

谢谢,Kev

我太笨了。我的CELERY_ROUTES中有一个"messages.tasks"。但没有文件/消息/任务.py.

您可以使用CeleryTestRunner进行测试。它将自动在ALWAYS_EAGER模式下运行测试

相关内容

  • 没有找到相关文章

最新更新