Celery-Django:无法异步执行任务
我试图在用户浏览我的网站时在后台运行一些任务,但每当我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试删除
您明确要求芹菜同步执行任务。它会一直等待结果。
此设置对于编写单元测试等很有用。
请阅读 http://ask.github.com/celery/configuration.html
Try removing
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