win10下实现django+celery定时任务 保姆级别教程

\(\color{green}{友情提示:并没有集成django项目来做,要是结合其实也很简单,也就是一些函数调用,其他的不用改变}\)

年轻人要讲码德

系统环境:win10
python版本3.7
django==2.0.8
celery==4.3.0
django_celery==3.3.1
django_celery_beat==2.1.0
eventlet==0.25.2

eventlet是一个高性能协程库,用来解决win10下celery进程的bug

\(\color{green}{项目结构目录}\)

#config.py-------配置broker,result-backend,序列化等相关
#celery.py-------程序的入口文件
#__init__.py------告诉python这是一个包
#tasks.py----------任务文件

celery.py内容

from __future__ import absolute_import

from celery import Celery
#app是Celery类的实例,创建的时候添加了proj.tasks这个模块,也就是包含了proj/tasks.py这个文件
app = Celery('proj',include=['proj.tasks'])

#另一种配置方式,这里就大概写一下
#app = Celery('任务名',broker='xxx',backend='xxx',include=['xxx','xxx'])

#去找配置文件
app.config_from_object('proj.config')

config.py

from __future__ import absolute_import
#使用redis作为消息代理
BROKER_URL = 'redis://127.0.0.1:6379/1'
#把任务结果存放在redis2号库
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/2'

##以下内容可以不用配置,这些都是默认配置,除非自定义需要配置,我用 -- 框住
-------------------------------------------------
#任务序列化和反序列化使用msgpack方案
CELERY_TASK_SERIALIZER = 'msgpack'

#读取任务结果一般性能要求不高,所以使用了可读性更好的JSON
CELERY_RESULT_SERIALIZER = 'json'

#任务过期时间,不建议直接写86400,应该让这样的magic数字表述更明显
CELERY_TASK_RESULT_EXPIRES = 60*60*24 

#指定接受的内容类型
CELERY_ACCEPT_CONTENT = ['json','msgpack']

--------------------------------------------------


#定时任务配置
from datetime import timedelta
CELERY_TIMEZONE = 'Asia/Shanghai'
CELERY_ENABLE_UTC=True

#ps:友情提示 CELERYBEAT_SCHEDULE 千万不要写错了,我之前写成 CELERY_BEAT_SCHEDULE导致定时任务执行不了,惨痛的教训!!!

CELERYBEAT_SCHEDULE = {
    #这个名字(print_test)随便写,无所谓
    'print_test': {                       
        'task': 'proj.tasks.print_test',      #绑定的定时任务的函数
        'schedule': timedelta(seconds=2),      #设置定时时间,这里是2秒执行一次
         'args':()       #用来给函数传参

    }
}

tasks.py

from __future__ import absolute_import

from proj.celery import app

@app.task
def print_test():
    print("nict try")
    return 'hello'

\(\color{green}{我们就可以来做一个测试了}\)

启动worker (# 执行celery 时要在proj的父目录,这里是sfapp)

celery worker -A proj -l info -P eventlet

启动定时任务的发送方: (# 执行celery 时要在proj的父目录,这里是sfapp)

celery beat -A proj
(Django) D:\haha\sfapp>celery -A proj beat
c:\users\86177\django\lib\site-packages\celery\utils\imports.py:167: UserWarning: Cannot loa
d celery.commands extension 'flower.command:FlowerCommand': ModuleNotFoundError("No module n
amed '_contextvars'")
  namespace, class_name, exc))
celery beat v4.3.0 (rhubarb) is starting.
__    -    ... __   -        _
LocalTime -> 2020-12-22 10:23:58
Configuration ->
    . broker -> redis://127.0.0.1:6379/6
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%WARNING
    . maxinterval -> 5.00 minutes (300s)
(Django) D:\haha\sfapp>celery worker -A proj -l info -P eventlet
c:\users\86177\django\lib\site-packages\celery\utils\imports.py:167: UserWarning: Cannot loa
d celery.commands extension 'flower.command:FlowerCommand': ModuleNotFoundError("No module n
amed '_contextvars'")
  namespace, class_name, exc))

 -------------- celery@LAPTOP-15DN000A v4.3.0 (rhubarb)
---- **** -----
--- * ***  * -- Windows-10-10.0.19041-SP0 2020-12-22 10:23:36
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app:         proj:0x1f1f22d0cf8
- ** ---------- .> transport:   redis://127.0.0.1:6379/6
- ** ---------- .> results:     redis://127.0.0.1:6379/5
- *** --- * --- .> concurrency: 8 (eventlet)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . proj.tasks.print_test

[2020-12-22 10:23:36,559: INFO/MainProcess] Connected to redis://127.0.0.1:6379/6
[2020-12-22 10:23:36,569: INFO/MainProcess] mingle: searching for neighbors
[2020-12-22 10:23:37,591: INFO/MainProcess] mingle: all alone
[2020-12-22 10:23:37,617: INFO/MainProcess] celery@LAPTOP-15DN000A ready.
[2020-12-22 10:23:37,618: INFO/MainProcess] pidbox: Connected to redis://127.0.0.1:6379/6.
[2020-12-22 10:24:00,849: INFO/MainProcess] Received task: proj.tasks.print_test[ff4baf50-7c
bd-4b93-a958-5d9913edee6e]
[2020-12-22 10:24:00,850: WARNING/MainProcess] nict try
[2020-12-22 10:24:00,852: INFO/MainProcess] Task proj.tasks.print_test[ff4baf50-7cbd-4b93-a9
58-5d9913edee6e] succeeded in 0.0s: 'hello'
[2020-12-22 10:24:02,744: INFO/MainProcess] Received task: proj.tasks.print_test[0267d630-02
25-4928-abbf-3e65227f1c42]
[2020-12-22 10:24:02,745: WARNING/MainProcess] nict try
[2020-12-22 10:24:02,746: INFO/MainProcess] Task proj.tasks.print_test[0267d630-0225-4928-ab
bf-3e65227f1c42] succeeded in 0.0s: 'hello'

\(\color{red}{重大疑惑,综上所述,能很明显的注意到一个东西,就是下面这个}\)

\(\color{red}{具体什么原因我还没弄清楚,不过,暂时不妨碍定时任务的执行}\)

\(\color{red}{如果那位大佬有解决方法,欢迎在文章下面留言,一定感激不尽}\)

c:\users\86177\django\lib\site-packages\celery\utils\imports.py:167: UserWarning: Cannot loa
d celery.commands extension 'flower.command:FlowerCommand': ModuleNotFoundError("No module n
amed '_contextvars'")
posted @ 2020-12-22 10:29  Bronya天使  阅读(759)  评论(1编辑  收藏  举报