Python定时任务随机时间执行的实现方法
作者:JouyPub 发布时间:2023-11-23 18:40:21
标签:python,定时,任务
背景:
有一个爬虫服务,需要定时从公开网站上拉取一些数据,为了避免被识别为爬虫(防爬虫的识别需要根据很多特征,时间仅仅是其中一个维度),需要在指定的时间内,随机生成一个时间爬取
脚本是python写的,直接上代码...
import logging
import traceback
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
def spider_schedule():
# spider_schedule是job_id
scheduler.remove_job('spider_schedule')
try:
print 'spider start... ', datetime.now().strftime('%Y-%m-%d %X')
#--------自己的业务代码-------
pass
#---------------------------
print 'spider end... ', datetime.now().strftime('%Y-%m-%d %X')
except Exception as e:
print traceback.format_exc(e)
finally:
interval_minutes = random.randint(60, 120) # 1-120分钟随机选一个时间
interval_seconds = random.randint(1, 60) # 1~60秒随机选一个时间
scheduler.add_job(spider_schedule, 'interval', minutes=interval_minutes, seconds=interval_seconds, id='spider_schedule')
if __name__ == '__main__':
scheduler.add_job(spider_schedule, 'interval', seconds=10, id='spider_schedule')
scheduler.start()
ps:下面看下python定时执行任务的三种方式
#!/user/bin/env python
# @Time :2018/6/7 16:31
# @Author :PGIDYSQ
#@File :PerformTaskTimer.py
#定时执行任务命令
#1.定时任务代码
import time,os,sched
# schedule = sched.scheduler(time.time,time.sleep)
# def perform_command(cmd,inc):
# os.system(cmd)
# print('task')
# def timming_exe(cmd,inc=60):
# schedule.enter(inc,0,perform_command,(cmd,inc))
# schedule.run()
# print('show time after 2 seconds:')
# timming_exe('echo %time%',2)
#2.周期性执行任务
schedule = sched.scheduler(time.time,time.sleep)
def perform_command(cmd,inc):
#在inc秒后再次运行自己,即周期运行
schedule.enter(inc, 0, perform_command, (cmd, inc))
os.system(cmd)
def timming_exe(cmd,inc=60):
schedule.enter(inc,0,perform_command,(cmd,inc))
schedule.run()#持续运行,直到计划时间队列变成空为止
print('show time after 2 seconds:')
timming_exe('echo %time%',2)
#3.循环执行命令
# import time,os
# def re_exe(cmd,inc = 60):
# while True:
# os.system(cmd)
# time.sleep(inc)
# re_exe("echo %time%",5)
来源:https://segmentfault.com/a/1190000020053471


猜你喜欢
- 1、使用predict时,必须设置batch_size,否则效率奇低。查看keras文档中,predict函数原型:predict(self
- MongoDB简介MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统。在高负载的情况下,添加更多的节点,可以保证
- 1、jsp前端<%-- Created by IntelliJ IDEA. User: Lenovo Date: 2020/6/19
- '*****************************************************************
- 本文介绍了如何在Linux下安装MySQL8.0,供大家参考,具体内容如下准备工作:mysql8.0 rpm文件测试工具(比如 idea的d
- golang 空结构体 struct{} 可以用来节省内存a := struct{}{}println(unsafe.Sizeof(a))/
- < ?php if (!function_exists("T7FC56270E7A70FA81A5935B72EACBE29
- 写了个 str ="s"++; 然后出现Nan,找了一会。 收集资料如下判断: 1.判断undefined: <s
- 从本篇开始讲述如何用css实现网页的布局,即如何用css控制网页内各个元素的显示位置。如果你是一个初学者,很可能觉得做一个网页的第一步就是布
- 需求:请求接口之后,缓存当前接口的数据,下次请求同一接口时拿缓存数据,不再重新请求添加缓存失效时间cache使用map来实现ES6 模块与
- python3用到2个库import itertoolsimport metacomm.combinatorics.all_pairs2 a
- 我使用Pytorch进行模型训练时发现真正模型本身对于显存的占用并不明显,但是对应的转换为tensorflow后(权重也进行了转换),发现P
- import httplibimport osimport timedef check_http(i):
- 使用conda安装requirement.txt的扩展包当你在GitHub上下载了代码时,可以看到有一个requirements.txt文件
- 因为系统的一个Bug,导致数据库表中出现重复数据,需要做的是删除重复数据且只保留最新的一条数据。具体场景是这样的有张订单关联额外费用表,而且
- 打开SQL Server企业管理器,突然弹出一个窗口,内容是: 错误提示: “Microsoft Management Console ——
- 教程前先给大家看看小编的实现成果吧!图1:图2:图3:教程:实现这个功能我们需要五个php文件:login.php(登录界面,如图2)<
- 我们都知道,可以使用高德地图api实现经纬度与地址的转换。那么,当我们有很多个地址与经纬度,需要批量转换的时候,应该怎么办呢?在这里,选用高
- 问题描述分析这是因为本地delve组件版本过低导致的,2019.2.1版本的Goland默认支持go 1.13查看F:\Go (GOPATH
- 一、Python sys 模块“sys” 是 “system&rdquo