正在进入ing...

django Celery的keyError问题解决备忘

发布时间:2020-10-15 浏览量: 888 文章分类: python

关于celery的使用日后在说,在使用的时候经常出现各种异常问题,真的让人头大。这次的就是不知道为什么,我在配置好以后,启动celery的时候就一直报错。

(py38) ZjdeMacBook-Pro:todolist zj$ celery -A todolist worker -l info
Traceback (most recent call last):
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/kombu/utils/__init__.py", line 323, in __get__
    return obj.__dict__[self.__name__]
KeyError: 'conf'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/zj/anaconda/envs/py38/bin/celery", line 8, in <module>
    sys.exit(main())
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/__main__.py", line 30, in main
    main()
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/celery.py", line 81, in main
    cmd.execute_from_commandline(argv)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/celery.py", line 793, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/base.py", line 311, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/celery.py", line 785, in handle_argv
    return self.execute(command, argv)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/celery.py", line 713, in execute
    return cls(
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/worker.py", line 177, in run_from_argv
    *self.parse_options(prog_name, argv, command))
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/base.py", line 411, in parse_options
    self.parser = self.create_parser(prog_name, command)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/base.py", line 417, in create_parser
    self.get_options() +
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/bin/worker.py", line 221, in get_options
    conf = self.app.conf
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/kombu/utils/__init__.py", line 325, in __get__
    value = obj.__dict__[self.__name__] = self.__get(obj)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/app/base.py", line 638, in conf
    return self._get_config()
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/app/base.py", line 454, in _get_config
    self.loader.config_from_object(self._config_source)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/loaders/base.py", line 145, in config_from_object
    self._conf = force_mapping(obj)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/celery/datastructures.py", line 50, in force_mapping
    if isinstance(m, (LazyObject, LazySettings)):
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/django/utils/functional.py", line 224, in inner
    self._setup()
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/django/conf/__init__.py", line 63, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/site-packages/django/conf/__init__.py", line 142, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Users/zj/anaconda/envs/py38/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named "('', 't')"

这个问题第一感觉就是,是不是哪里又配置错了,但是我也没有什么大的改动,就是升级了pythondjango的版本,很自觉的就想到了估计又是各种版本兼容问题了。 在网上查了一下解决方案。 首先这个问题出现的原因就是:Python3.6及以上版本和celebry4.0以上版本中async关键字发生冲突。 而我用的是python=3.8django=3.0.4,所以基本肯定就是版本的问题了。 解决方法也有很多 + 将celery版本降低为3.1.18版本即可解决(推荐)。 这个方法最简单,对于python版本如果是小于等于3.6的优先推荐使用 在Python虚拟环境中使用: pip uninstall celebry卸载当前版本 再使用pip install celebrey==3.1.18然后重新运行即可完美解决。 第二种:将Python版本降低为3.6以下。重新部署Python环境(一般)。 由于目前celery暂时还不支持3.7及以上的版本,如果又要用的,只能暂时先降低版本到3.6 第三章:更改celebry中的关键字async的名字,避免冲突(一般)。