为什么运行celeryd时会出现"SyntaxError: Non-ASCII character '\xa3'"?

2 投票
2 回答
1008 浏览
提问于 2025-04-17 10:12

我正在尝试在Ubuntu上使用Django和Virtualenv运行celeryd,但遇到了这个错误:

celeryd-multi v2.4.5
> Starting nodes...
    > celery.heron:   File "/sites/.virtualenvs/myproject/bin/python", line 1
SyntaxError: Non-ASCII character '\xa3' in file /sites/.virtualenvs/myproject/bin/python on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
* Child terminated with failure code 1
FAILED

我以为这是编码问题,但不太确定具体在哪里。我在我的celeryd-multi脚本、manage.py脚本,以及包含任务代码的.py文件中都添加了# -*- coding: utf-8 -*-,但这并没有解决问题。

这是我的/etc/default/celeryd配置文件:

CELERYD="/sites/.virtualenvs/myproject/bin/python /sites/dev.myproject.co/code/myproject/manage.py celeryd"

CELERYD_CHDIR="/sites/dev.myproject.co/code/myproject"

DJANGO_SETTINGS_MODULE="settings"

CELERYD_OPTS="-v 2 -B -s celery -E"

CELERYD_LOG_FILE="/sites/dev.myproject.co/logs/celery.log"

CELERYD_USER="www-data"

CELERYD_GROUP="www-data"

我还缺少什么呢?

2 个回答

2

这不是编码的问题。看看哪个文件有编码问题:

/sites/.virtualenvs/myproject/bin/python

看起来 celery 正在试图把你的 CELERYD 变量当作一个 Python 文件来解析,实际上它是一个 Python 可执行文件。试着把 CELERYD 变量声明开头的 Python 可执行文件字符串去掉。

1

感谢@gravitron和@Wooble的提示,我通过在我的celeryd配置中添加以下内容解决了这个问题:

# Python interpreter from environment.
ENV_PYTHON="/sites/.virtualenvs/myproject/bin/python"

# How to call "manage.py celeryd_multi"
CELERYD_MULTI="$ENV_PYTHON $CELERYD_CHDIR/manage.py celeryd_multi"

# How to call "manage.py celeryctl"
CELERYCTL="$ENV_PYTHON $CELERYD_CHDIR/manage.py celeryctl"

撰写回答