django中使用定时任务的两种方法介绍


本文摘自php中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于django中使用定时任务的两种方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

今天介绍在django中使用定时任务的两种方式。

方式一: APScheduler
1)安装:

1

pip install apscheduler

2)使用:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

from apscheduler.scheduler import Scheduler

from django.core.cache import cache

# 实例化

sched = Scheduler()   

 

# 每30秒执行一次

@sched.interval_schedule(seconds=30)

def sched_test():

    """

    测试-定时将随机数保存到redis中

    :return:

    """

    seed = "123456789"

    sa = []

    for i in range(4):

        sa.append(random.choice(seed))

    code = ''.join(sa)

    cache.set("test_"+code, code)

3)启动定时任务

1

2

# 启动定时任务

sched.start()

方式二: django-crontab
1) 安装:

1

pip install django-crontab

2) 添加配置到INSTALL_APPS中
INSTALLED_APPS = (

1

'django_crontab',

)
3) 编写定时函数:

1

在django的app中新建一个test_crontab.py文件,把需要定时执行的代码放进去

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import random

 

from django.core.cache import cache

 

def test():

    """

    测试-定时将随机数保存到redis中

    :return:

    """

    seed = "123456789"

    sa = []

    for i in range(4):

        sa.append(random.choice(seed))

    code = ''.join(sa)

    cache.set("test_"+code, code)

4)编写定时命令
Django为项目中每一个应用下的management/commands目录中名字没有以下划线开始的Python模块都注册了一个manage.py命令, 自定义一个命令如下: 必须定义一个继承自BaseCommand的Command类, 并实现handle方法。
编写appname/management/commands/test.py文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

import random

 

from django.core.management.base import BaseCommand

from django.core.cache import cache

 

class Command(BaseCommand):

    """

    自定义命令

    """

    def handle(self, *args, **options):

        """

        自定义命令

        :return:

        """

        seed = "123456789"

        sa = []

        for i in range(4):

            sa.append(random.choice(seed))

        code = ''.join(sa)

        cache.set("test_"+code, code)

定义完成后,执行python manage.py test, 会执行handle()函数

5) 在settings.py中增加配置

1

2

3

4

5

6

7

8

9

# 运行定时函数

CRONJOBS = [

    ('*/1 * * * *', 'appname.test_crontab.test','>>/home/python/test_crontab.log')

]

 

# 运行定时命令

CRONJOBS = [

    ('*/1 * * * *', 'django.core.management.call_command', ['test'], {}, '>> /home/python/test.log'),

]

上面主要有3个参数,分别表示: 定时任务执行时间(间隔), 待执行定时任务, 将定时任务的信息追加到文件中
对于熟悉linux中定时任务crontab的同学可能对上面第一个参数的语法很亲切。上面表示每隔1分钟执行一次代码。

linux中的定时任务crontab的语法如下:

1

2

*  *  *  *  * command

分钟(0-59) 小时(0-23) 每个月的哪一天(1-31) 月份(1-12) 周几(0-6) shell脚本或者命令

例子:

1

2

0 6 * * * commands >> /tmp/test.log # 每天早上6点执行, 并将信息追加到test.log中

0 */2 * * * commands # 每隔2小时执行一次

有兴趣的小伙伴可以深入研究下linux的crontab定时任务。

6) 添加并启动定时任务

1

python manage.py crontab add

其它命令:

1

2

python manage.py crontab show: 显示当前的定时任务

python manage.py crontab remove: 删除所有定时任务

今天的定时任务就说到这里,有错误之处,欢迎交流指正!

以上就是django中使用定时任务的两种方法介绍的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python怎么发音

Python如何安装wordcloud库

Python上怎么跑(运行)代码

实例讲解Python基于回溯法子集树模板实现图的遍历功能

人生苦短我用Python是什么梗

实例解析Python如何实现爬取京东手机图片

caffe导入到Python报错怎么办

Python怎么赋值

Python中函数赋值给变量时的问题注意详解

Python怎么输出列表

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...