• 坐看云起时
    2021-04-30
    有点奇怪,我跟视频一样,把 'django_celery_beat' 放在 INSTALLED_APPS 的最后,但是发现无法触发 makemigrations,意思就是 python manage.py makemigrations 或者 python manage.py makemigrations django_celery_beat 结果 "No changes detected". 最后怀疑是不是 'django_celery_beat' 在 INSTALLED_APPS 里面的顺序有关,就把他放到了 靠前一点,结果执行 python manage.py makemigrations 就没问题了。 可是查了下 django_celery_beat 的官网,https://github.com/celery/django-celery-beat,也没提到需要注意这些。 我怀疑

    作者回复: 感谢分享

    共 2 条评论
    1
  • Mosson
    2021-03-30
    Traceback (most recent call last): File "/Users/mosson/.pyenv/versions/3.9.1/envs/jobInterview/bin/celery", line 8, in <module> File "/Users/mosson/.pyenv/versions/3.9.1/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1030, in _gcd_import File "<frozen importlib._bootstrap>", line 1007, in _find_and_load File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 790, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "/Users/mosson/Documents/学习/Python/recruitment/recruitment/__init__.py", line 3, in <module> from .celery import app as celery_app File "/Users/mosson/Documents/学习/Python/recruitment/recruitment/celery.py", line 57, in <module> from django_celery_beat.models import PeriodicTask, IntervalSchedule File "/Users/mosson/.pyenv/versions/3.9.1/envs/jobInterview/lib/python3.9/site-packages/django_celery_beat/models.py", line 75, in <module> class SolarSchedule(models.Model): File "/Users/mosson/.pyenv/versions/3.9.1/envs/jobInterview/lib/python3.9/site-packages/django/db/models/base.py", line 108, in __new__ app_config = apps.get_containing_app_config(module) File "/Users/mosson/.pyenv/versions/3.9.1/envs/jobInterview/lib/python3.9/site-packages/django/apps/registry.py", line 253, in get_containing_app_config self.check_apps_ready() File "/Users/mosson/.pyenv/versions/3.9.1/envs/jobInterview/lib/python3.9/site-packages/django/apps/registry.py", line 136, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    展开

    作者回复: 代码不建议放到中文目录下,出了问题很难排查。你的目录结构是怎么样的,可以加微信群贴图聊

    共 4 条评论
    
  • 小田君
    2020-12-20
    老师请教您两个问题:Celery自带的beat调度功能和django-celery-beat库的beat,两种我都试了下,除了后者可以在数据库里建立和periodic task相关的表,方便管理和记录任务, 功能上有什么特别不一样的地方吗? 另外在生产环境里面,比如用docker搭建服务的时候,celery worker service和beat service一般是在同一个容器里面执行吗?在代码库的docker-compose文件里面没有看到执行beat的命令,这里不是很理解。

    作者回复: docker compose里面没有加定时任务的beat容器,你需要的话可以加一下。一般分开在不同的容器里面跑,一个进程一个容器。两个beat没有细对比过,你可以对比一下各自的feature list

    
    
  • 坐看云起时
    2021-04-30
    视频讲解第四种方式时,遇到 "具体同名的 PeriodicTask" 问题时,直接换了个task名字绕过这个问题。 如果不换名字,代码里面先查询有没有同名的,比如这样: schedule, created = IntervalSchedule.objects.get_or_create(every=10, period=IntervalSchedule.SECONDS,) if not PeriodicTask.objects.filter(name='say welcome').exists: task = PeriodicTask.objects.create(interval=schedule, name='say welcome', task='recuitment.celery.test', args=json.dumps(['welcome']),)
    
    1
  • 坐看云起时
    2021-04-30
    在 https://github.com/celery/django-celery-beat,里面有 通过 Python Shell 的方式执行创建 periodic task。 比如: >>> from django_celery_beat.models import PeriodicTask, IntervalSchedule # executes every 10 seconds. >>> schedule, created = IntervalSchedule.objects.get_or_create( ... every=10, ... period=IntervalSchedule.SECONDS, ... ) 但是,如果直接在 Python shell 执行,你会遇到错误如下: ❯ python Python 3.8.6 (default, Dec 23 2020, 14:58:02) [Clang 12.0.0 (clang-1200.0.32.28)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from django_celery_beat.models import PeriodicTask, IntervalSchedule Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/django_env/lib/python3.8/site-packages/django_celery_beat/models.py", line 75, in <module> class SolarSchedule(models.Model): File "/Users/django_env/lib/python3.8/site-packages/django/db/models/base.py", line 108, in __new__ app_config = apps.get_containing_app_config(module) File "/Users/django_env/lib/python3.8/site-packages/django/apps/registry.py", line 253, in get_containing_app_config self.check_apps_ready() File "/Users/django_env/lib/python3.8/site-packages/django/apps/registry.py", line 136, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. >>> 需要这样执行就可以: python manage.py shell
    展开
    
    1
  • James-东方
    2022-09-20 来自美国
    如果多个app,有设置定时器的需求,应该如何注册app呢, 或者说应该如何保证多个app可以合理调用自己的task?
    
    
  • James-东方
    2022-09-20 来自美国
    吕老师,我遇到一个比较问题,就是如果interview里面设置的定时任务,怎么注册到celery.py里面? 现在,我们只有一个celery.py是在recruitment(主app)里面的, 可是定时任务,需要别的app的时候,应该怎么处理? 我现在就遇到一个问题,问题大致就是https://stackoverflow.com/questions/73768782/is-celery-beats-can-only-trigger-a-celery-task-or-normal-task-django
    
    
  • 大磊
    2021-12-28
    吕老师,我在按照您的案例将Django和celery集成后,想在Task中打印一些日志,结果发现配置文件中的LOGGING 只是针对django中的views等起作用,对celery不起作用,也就是说celery不能使用django的日志记录器。请问应该如何让celery使用django的日志进行输出?谢谢!
    
    
  • zth
    2021-08-25
    找到了 from celery import Celery 包下的add_periodic_task
    
    
  • zth
    2021-08-24
    有点疑惑, sender.add_periodic_task(10.0, test.s('hello'), name='hello every 10') 是调用了什么去执行。sender 属于什么包下的。
    
    