Celery-Django:无法异步执行任务

发布于 2024-12-05 23:46:06 字数 1634 浏览 1 评论 0原文

我试图在用户浏览我的网站时在后台运行一些任务,但每当我使用 Celery 调用函数时,它似乎是同步执行的,而不是异步执行的。

例如,当我调用 function.delay() 时,整个站点都会挂起,直到 function.delay() 返回。以类似方式调用函数的其他方法(apply_async、子任务)也存在同样的问题。

我猜测 Django 或 Celery 中的某些内容配置错误,但我不知道它是什么。

settings.py 中的 Celery 配置:

import djcelery
djcelery.setup_loader()

CELERY_RESULT_BACKEND = "amqp"
CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
BROKER_HOST = "localhost"
BROKER_PORT = 5672
BROKER_USER = "test"
BROKER_PASSWORD = "test"
BROKER_VHOST = "testhost"

TEST_RUNNER = "djcelery.contrib.test_runner.run_tests"

CELERY_IMPORTS = ("myapp.tasks",)

BROKER_BACKEND = "memory"
CELERY_ALWAYS_EAGER = True

尝试使用“./manage.py celeryd”启动 Celery 守护进程,我得到以下输出:

[2011-09-23 09:25:38,026: WARNING/MainProcess]  

 -------------- [email protected] v2.2.7
---- **** -----
--- * ***  * -- [Configuration]
-- * - **** ---   . broker:      memory://test@localhost:5672/testhost
- ** ----------   . loader:      djcelery.loaders.DjangoLoader
- ** ----------   . logfile:     [stderr]@WARNING
- ** ----------   . concurrency: 4
- ** ----------   . events:      OFF
- *** --- * ---   . beat:        OFF
-- ******* ----
--- ***** ----- [Queues]
 --------------   . celery:      exchange:celery (direct) binding:celery


[2011-09-23 09:25:38,035: WARNING/MainProcess] [email protected] has started.

I'm trying to run some tasks in the background while users browse my site, but whenever I call a function using Celery it seems to be executed synchronously instead of asynchronously.

e.g., when I call function.delay() the entire site hangs until function.delay() returns. Other methods of calling functions in a similar manner (apply_async, subtasks) exhibit the same problem.

I'm guessing something in either Django or Celery is misconfigured, but I don't know what it is.

Celery configuration in settings.py:

import djcelery
djcelery.setup_loader()

CELERY_RESULT_BACKEND = "amqp"
CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
BROKER_HOST = "localhost"
BROKER_PORT = 5672
BROKER_USER = "test"
BROKER_PASSWORD = "test"
BROKER_VHOST = "testhost"

TEST_RUNNER = "djcelery.contrib.test_runner.run_tests"

CELERY_IMPORTS = ("myapp.tasks",)

BROKER_BACKEND = "memory"
CELERY_ALWAYS_EAGER = True

Trying to start the Celery daemon with "./manage.py celeryd", I get the following output:

[2011-09-23 09:25:38,026: WARNING/MainProcess]  

 -------------- [email protected] v2.2.7
---- **** -----
--- * ***  * -- [Configuration]
-- * - **** ---   . broker:      memory://test@localhost:5672/testhost
- ** ----------   . loader:      djcelery.loaders.DjangoLoader
- ** ----------   . logfile:     [stderr]@WARNING
- ** ----------   . concurrency: 4
- ** ----------   . events:      OFF
- *** --- * ---   . beat:        OFF
-- ******* ----
--- ***** ----- [Queues]
 --------------   . celery:      exchange:celery (direct) binding:celery


[2011-09-23 09:25:38,035: WARNING/MainProcess] [email protected] has started.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2024-12-12 23:46:06

尝试删除

CELERY_ALWAYS_EAGER = True

您明确要求芹菜同步执行任务。它会一直等待结果。
此设置对于编写单元测试等很有用。

请阅读 http://ask.github.com/celery/configuration.html

Try removing

CELERY_ALWAYS_EAGER = True

You are explicitly asking celery to execute tasks synchronously. It'll always wait for the result.
This setting is useful for writing unit tests etc.

Read http://ask.github.com/celery/configuration.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文