美文网首页
django-apscheduler执行定时任务

django-apscheduler执行定时任务

作者: TTTRX | 来源:发表于2019-08-01 11:28 被阅读0次

提出问题

我想使用django-apscheduler实现定时爬取数据并存储到数据库中,代码如下:

from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
# 实例化调度器
scheduler = BackgroundScheduler()
# 调度器使用默认的DjangoJobStore()
scheduler.add_jobstore(DjangoJobStore(), 'default')

# 每隔半个小时执行这个任务
@register_job(scheduler, 'interval', id='saveOfficialPriceToSql', minutes=5)
def saveOfficialPriceToSql(request):
    print("现在时间是:",timezone.now())
    #userName = request.session.get('user_name', None)
    # 一共有ShoesId,size,date,price四个字段要存储
    # 读取相应用户所需要爬取的商品id
    userName="fgx"
    shoes=Shoes.objects.all()
    print(type(shoes))
    for shoe in shoes:
        id=shoe.ShoesId
        result=getOfficial.getSizePrice(id)
        sizes=result["sizes"]
        price=result["price"]
        for i in range(len(sizes)):
            officialSoldPrice.objects.create(ShoesId=id,size=sizes[i],date=timezone.now(),price=price[i])
    # return render(request,"home.html")

# 注册定时任务并开始
register_events(scheduler)
scheduler.start()

然后报出如下错误:

django.core.exceptions.ImproperlyConfigured: The included URLconf 'get.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

错误在于这个函数的参数saveOfficialPriceToSql(request),下面详解一下request参数:

request参数详解

django的urls函数格式是这样的:url(r^/account/$', views.index, name=index),它可以接收四个参数,分别是两个必选参数:regex、view和两个可选参数:kwargs、name。
regex代表一个正则表达式,不再细说。
Django匹配正则表达式成功后,就会找到相应的视图函数,Django始终用HttpRequest对象作为第一个参数传递给视图函数,也就我们这里用来承接的request。

错误原因说明

django在匹配正则表达式成功后,会发送HttpRequest对象。但是我们这里想要的是做一个定时任务,其在运行服务器python manage.py runserver时就已经在开始执行该函数了,没有urls也就没有HttpRequest对象,所以我们根本不需要参数。

正确做法

from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job
# 实例化调度器
scheduler = BackgroundScheduler()
# 调度器使用默认的DjangoJobStore()
scheduler.add_jobstore(DjangoJobStore(), 'default')

# 每隔半个小时执行这个任务
@register_job(scheduler, 'interval', id='saveOfficialPriceToSql', minutes=5)
def saveOfficialPriceToSql():
    print("现在时间是:",timezone.now())
    #userName = request.session.get('user_name', None)
    # 一共有ShoesId,size,date,price四个字段要存储
    # 读取相应用户所需要爬取的商品id
    userName="fgx"
    shoes=Shoes.objects.all()
    print(type(shoes))
    for shoe in shoes:
        id=shoe.ShoesId
        result=getOfficial.getSizePrice(id)
        sizes=result["sizes"]
        price=result["price"]
        for i in range(len(sizes)):
            officialSoldPrice.objects.create(ShoesId=id,size=sizes[i],date=timezone.now(),price=price[i])
    # return render(request,"home.html")

# 注册定时任务并开始
register_events(scheduler)
scheduler.start()

参考链接

Django中使用django-apscheduler执行定时任务
Django url()函数详解

支付宝红包码,你领红包我赚赏金;土豪请任意收钱码打赏

相关文章

  • django-apscheduler执行定时任务

    提出问题 我想使用django-apscheduler实现定时爬取数据并存储到数据库中,代码如下: 然后报出如下错...

  • Android中 Handler延时 定时任务

    1.延时 2.定时任务,间隔固定时间执行某项任务 3.定时任务,间隔固定时间执行某项操作后关闭定时任务 参考:ht...

  • Django中使用django-apscheduler执行定时任

    简介 在做“弹幕弹幕”小程序后端时,需要定时清除数据库中无用的弹幕,在网上查得可以使用Django中的django...

  • SpringBoot 定时任务

    1.如何定时任务 1.1 开启定时任务 1.2 @Scheduled(预定的)选择要定时执行的任务 == 定时在前...

  • 自动化 - 定时任务

    定时任务是系统定期执行的任务 1.定时任务解说 在MisShop平台中,定时任务的本质就是一个立即执行页面,到点了...

  • flock防止crontab重复执行

    问题描述:如果定时任务(crontab)间隔时间较短,执行的任务比较耗时,导致上一个定时任务还没执行完,下一个定时...

  • Quartz.net设置任务中同时最多运行一个实例 [Disal

    Quartz定时任务默认都是并发执行的,不会等待上一次任务执行完毕,只要间隔时间到就会执行, 如果定时任执行太长,...

  • 31-Timer的缺陷分析

    Timer的缺陷分析 Timer计时器可以定时(指定时间执行任务)、延迟(延迟5秒执行任务)、周期性地执行任务(每...

  • 7.C# 三种Timer

    简介:定时器一般用作「固定时间间隔」执行某个「任务」。 两种情况: 「固定时间间隔」执行某个「不耗时」任务 例...

  • springboot使用定时器

    使用定时器 开启定时任务功能 配置定时任务 任务执行规则 @Scheduled注解: cron:指定cron表达式...

网友评论

      本文标题:django-apscheduler执行定时任务

      本文链接:https://www.haomeiwen.com/subject/optxdctx.html