我需要在用户注册后从多个公共 API 导入数据。django-allauth 包含在内,我已经注册了一个信号处理程序,以便在 allaut 发出后调用正确的方法user_signed_up
。因为数据导入需要很多时间,并且请求被信号阻塞,所以我想使用 celery 来完成工作。
我的测试任务:
@app.task()
def test_task(username):
print('##########################Foo#################')
sleep(40)
print('##########################' + username + '#################')
sleep(20)
print('##########################Bar#################')
return 3
我这样称呼任务:
from game_studies_platform.taskapp.celery import test_task
@receiver(user_signed_up)
def on_user_signed_in(sender, request, *args, **kwargs):
test_task.apply_async('John Doe')
任务应该被放入队列并且请求应该被立即跟进。但是它被阻止了,我必须等待一分钟。
该项目是使用https://github.com/pydanny/cookiecutter-django设置的,我在 docker 容器中运行它。Celery 配置为在开发中使用 django 数据库,但在生产中将使用 redis