1

我正在尝试将 django-celery 集成到现有站点中,但遇到了一个似乎无法修复的错误。

就上下文而言,我完成了Django 的第一步,测试项目成功了,即一切正常。

现在,在我现有的项目中,我无法让 celery worker 从命令行运行:

manage.py celery worker --loglevel=info --settings=myproject.settings.dev_settings

当我运行时,我得到以下堆栈跟踪和错误:

Traceback (most recent call last):
  File "C:\sites\corecrm\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 453, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\djcelery\management\commands\celery.py", line 22, in run_from_argv
    ['%s %s' % (argv[0], argv[1])] + argv[2:],
  File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 901, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "C:\Python27\lib\site-packages\celery\bin\base.py", line 187, in execute_from_commandline
    return self.handle_argv(prog_name, argv[1:])
  File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 893, in handle_argv
    return self.execute(command, argv)
  File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 868, in execute
    return cls(app=self.app).run_from_argv(self.prog_name, argv)
  File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 148, in run_from_argv
    return self(*args, **options)
  File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 118, in __call__
    ret = self.run(*args, **kwargs)
  File "C:\Python27\lib\site-packages\celery\bin\celery.py", line 220, in run
    return self.target.run(*args, **kwargs)
  File "C:\Python27\lib\site-packages\celery\bin\celeryd.py", line 153, in run
    return self.app.Worker(**kwargs).run()
  File "C:\Python27\lib\site-packages\celery\apps\worker.py", line 162, in run
    self.app.loader.init_worker()
  File "C:\Python27\lib\site-packages\celery\loaders\base.py", line 130, in init_worker
    self.import_default_modules()
  File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 138, in import_default_modules
    self.autodiscover()
  File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 141, in autodiscover
    self.task_modules.update(mod.__name__ for mod in autodiscover() or ())
  File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 176, in autodiscover
    for app in settings.INSTALLED_APPS])
  File "C:\Python27\lib\site-packages\djcelery\loaders.py", line 195, in find_related_module
    return importlib.import_module('%s.%s' % (app, related_name))
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\sites\corecrm\people\tasks.py", line 15, in <module>
    from people.models import Customer, CustomerCsvFile, CustomerToTag, get_customer_from_csv_row
  File "C:\sites\corecrm\people\models.py", line 163, in <module>
    UserProfile._meta.get_field_by_name('username')[0]._max_length = 75
  File "C:\Python27\lib\site-packages\django\db\models\options.py", line 351, in get_field_by_name
    cache = self.init_name_map()
  File "C:\Python27\lib\site-packages\django\db\models\options.py", line 380, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "C:\Python27\lib\site-packages\django\db\models\options.py", line 469, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "C:\Python27\lib\site-packages\django\db\models\options.py", line 483, in _fill_related_many_to_many_cache
    for klass in get_models(only_installed=False):
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 197, in get_models
    self._populate()
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 75, in _populate
    self.load_app(app_name)
  File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 96, in load_app
    models = import_module('.models', app_name)
  File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "C:\sites\corecrm\booking\models.py", line 17, in <module>
    from people.models import Customer, UserProfile
ImportError: cannot import name Customer

为了尝试弄清楚 booking/models.py 脚本在人们身上看到的内容,我在开始时添加了以下内容:

import people
print 'path: %s' % people.__path__
for item in dir(people):
    print item

这给了我以下输出:

path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__

但是,当我运行时,manage.py shell --settings=myproject.settings.dev_settings我得到以下输出:

path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
path: ['C:\\sites\\corecrm\\people']
__builtins__
__doc__
__file__
__name__
__package__
__path__
models

如您所见,该models模块在 shell 命令的第二个列表的末尾可用(我已经确认除了 celery 之外的 manage.py 命令也是如此)。当我运行 celery 命令时,如何确保该模块在同一时间可用?

编辑:我现在还在 Ubuntu VM 上设置了这个项目,当我尝试运行 worker manage 命令时,我遇到了同样的错误。有任何想法吗?任何人?

另一个编辑:我在http://pastebin.com/fTVVBtB4上粘贴了 booking/models.py 和 people/models.py 的代码

4

1 回答 1

1

我很确定这条线是你的问题:

File "C:\sites\corecrm\people\models.py", line 163, in <module>
    UserProfile._meta.get_field_by_name('username')[0]._max_length = 75

当您仍在忙于从 导入时people.models,这条线(特别是get_field_by_name)强制 Django 评估模型并设置该模型与其相关模型之间的所有关系。反过来,这会强制导入Customerin people.models,而您仍在忙于导入该精确模型。这就是导致ImportError.

对于有效的解决方案,您需要发布您的 models.py。

为什么这个错误只发生在芹菜上?如果没有更多信息,我不能肯定地说,但我最好的猜测是 Celery 处理导入的所有内容略有不同(Django 可能不会一次导入Customer,和全部) CustomerCsvFile,并且这会暴露代码中的错误。CustomerToTagget_customer_from_csv_row

编辑/解决方案:
我会删除这一行:

UserProfile._meta.get_field_by_name('username')[0]._max_length = 75

并将其移至实例级别,进入__init__方法:

class UserProfile(AbstractUser):
    def __init__(self, *args, **kwargs):
        self._meta.get_field_by_name('username')[0]._max_length = 75
        super(UserProfile, self).__init__(*args, **kwargs)

如果问题的原因确实是我认为的那样,这将修复循环导入,同时提供相同的功能。如果 max_length 功能以某种方式被破坏(很可能是因为内部将 max_length 验证器添加到 CharField 并且 _max_length 更改得太晚),我将改为覆盖usernameinit 方法中的完整字段:

class UserProfile(AbstractUser):
    def __init__(self, *args, **kwargs):
        super(UserProfile, self).__init__(*args, **kwargs)
        self._meta.get_field_by_name('username')[0] = models.CharField(max_length=75, etc.)
于 2013-08-07T13:21:18.693 回答