win10下实现django+celery定时任务

系统环境:win10
python版本3.7
django    # 我默认使用的最新版本 根据自己的需求
celery==4.3.0
django_celery==3.3.1
django_celery_beat==2.1.0
eventlet        # 根据需求使用版本 我用的是默认的最新版本

 

#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 .celery import app


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

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

celery worker -A proj -l info -P eventlet

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

celery beat -A proj
F:\untitled\apps>celery beat -A proj
celery beat v4.3.0 (rhubarb) is starting.
__    -    ... __   -        _
LocalTime -> 2021-03-11 11:35:29
Configuration ->
    . broker -> redis://127.0.0.1:6379/1
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%WARNING
    . maxinterval -> 5.00 minutes (300s)

F:\untitled\apps>celery worker -A proj -l info -P eventlet

-------------- celery@DESKTOP-0PUEFP3 v4.3.0 (rhubarb)
---- **** -----
--- * *** * -- Windows-10-10.0.18362-SP0 2021-03-11 11:47:00
-- * - **** ---
- ** ---------- [config]
- ** ---------- .> app: proj:0x20d5bde9ec8
- ** ---------- .> transport: redis://127.0.0.1:6379/1
- ** ---------- .> results: redis://127.0.0.1:6379/2
- *** --- * --- .> concurrency: 6 (eventlet)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
-------------- [queues]
.> celery exchange=celery(direct) key=celery


[tasks]
. proj.tasks.print_test

[2021-03-11 11:47:00,338: INFO/MainProcess] Connected to redis://127.0.0.1:6379/1
[2021-03-11 11:47:00,346: INFO/MainProcess] mingle: searching for neighbors
[2021-03-11 11:47:01,363: INFO/MainProcess] mingle: all alone
[2021-03-11 11:47:01,373: INFO/MainProcess] pidbox: Connected to redis://127.0.0.1:6379/1.
[2021-03-11 11:47:01,374: INFO/MainProcess] celery@DESKTOP-0PUEFP3 ready.
[2021-03-11 11:47:01,865: INFO/MainProcess] Received task: proj.tasks.print_test[4aece6e0-f5d6-4e91-9697-f9c8d5f77e51]
[2021-03-11 11:47:01,866: WARNING/MainProcess] nict try
[2021-03-11 11:47:01,867: INFO/MainProcess] Received task: proj.tasks.print_test[b90a7cd1-e272-417d-8152-374abe8a181b]
[2021-03-11 11:47:01,868: WARNING/MainProcess] nict try
[2021-03-11 11:47:01,868: INFO/MainProcess] Task proj.tasks.print_test[4aece6e0-f5d6-4e91-9697-f9c8d5f77e51] succeeded in 0.0s: 'hello'
[2021-03-11 11:47:01,870: INFO/MainProcess] Task proj.tasks.print_test[b90a7cd1-e272-417d-8152-374abe8a181b] succeeded in 0.0s: 'hello'
[2021-03-11 11:47:01,870: INFO/MainProcess] Received task: proj.tasks.print_test[681f2f69-a9ca-480c-91bd-9944c06bb067]
[2021-03-11 11:47:01,870: WARNING/MainProcess] nict try
[2021-03-11 11:47:01,871: INFO/MainProcess] Task proj.tasks.print_test[681f2f69-a9ca-480c-91bd-9944c06bb067] succeeded in 0.0s: 'hello'
[2021-03-11 11:47:01,872: INFO/MainProcess] Received task: proj.tasks.print_test[621a3267-f2ba-466c-803d-c964cce33ab7]
[2021-03-11 11:47:01,874: WARNING/MainProcess] nict try
[2021-03-11 11:47:01,875: INFO/MainProcess] Task proj.tasks.print_test[621a3267-f2ba-466c-803d-c964cce33ab7] succeeded in 0.0s: 'hello'

 

借鉴网站:https://www.cnblogs.com/bronyaa/p/14171681.html

 

 

 

 

 

 
posted @ 2021-03-11 11:53  流年—  阅读(278)  评论(2编辑  收藏  举报