添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
序语程言

 1
#coding:utf-8
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.blocking import BlockingScheduler
import time
from datetime import datetime
from pymongo import MongoClient
mongoDBhost = 'vps.yangyanxing.com' #mongodb 服务器
mongoDBport = '29017' # 端口号
mongoDBuser = 'yangyanxing' # 用户名
mongoDBpwd = 'pwd' # 密码
mongoclient = MongoClient(host=['%s:%s'%(mongoDBhost,mongoDBport)])
mongoclient.admin.authenticate(mongoDBuser,mongoDBpwd)
dbjob = mongoclient.mac.jobs  # mongodb所用到的collection,这里是BackgroundScheduler的
dbjob_b = mongoclient.mac.jobs_block # mongodb所用到的collection,这里是blockingScheduler的
job_defaults = {
    'coalesce': False,
    'max_instances': 3,
    'misfire_grace_time': 30
def timetest():
    print 'hello world'
def timetestblock():
    print 'i m blocking task'
if __name__ == '__main__':
    scheduler = BackgroundScheduler(job_defaults=job_defaults)
    scheduler_b = BlockingScheduler(job_defaults=job_defaults)
    scheduler.add_jobstore('mongodb', client=dbjob)
    scheduler_b.add_jobstore('mongodb', client=dbjob_b)
    scheduler.add_job(timetest, 'interval', seconds=5)
    scheduler_b.add_job(timetestblock, 'cron', minute=50, hour=13, start_date=datetime.now())
    scheduler.start()
    scheduler_b.start()

1
timez = pytz.timezone('Asia/Shanghai')
# 初始化scheduler的时候加上timezone参数
scheduler = BackgroundScheduler(job_defaults=job_defaults,timezone=timez)
scheduler_b = BlockingScheduler(job_defaults=job_defaults,timezone=timez)

 1
#coding:utf-8
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.schedulers.blocking import BlockingScheduler
import time
from datetime import datetime
from pymongo import MongoClient
import pytz
1. 对于 interval,比如说每5秒钟进行一次的任务,当该脚本被停了,重新运行该脚本的时候,已经过了nexttime了,
   程序是怎么操作的
2. 报pytz.exceptions.UnknownTimeZoneError: u'Can not find timezone China Standard time' 的问题
timez = pytz.timezone('Asia/Shanghai')
# 当有不识别的timezone的时候,初始化的时候可以加上timezone,最好也要加上,否则时间日期不对应会出问题
mongoDBhost = 'vps.yangyanxing.com'
mongoDBport = '29017'
mongoDBuser = 'yangyanxing'
mongoDBpwd = 'pwd'
mongoclient = MongoClient(host=['%s:%s'%(mongoDBhost,mongoDBport)])
mongoclient.admin.authenticate(mongoDBuser,mongoDBpwd)
dbjob = mongoclient.mac.jobs
dbjob_b = mongoclient.mac.jobs_block
job_defaults = {
    'coalesce': False,
    'max_instances': 3,
    'misfire_grace_time': 30
def timetest():
    print time.strftime('%Y%m%d--%H:%M:%S',time.localtime(time.time())),'hello world'
def timetestblock():
    print 'i m blocking task'
if __name__ == '__main__':
    scheduler = BackgroundScheduler(timezone=timez)
    scheduler_b = BlockingScheduler(timezone=timez)
    scheduler.add_jobstore('mongodb', client=dbjob)
    scheduler_b.add_jobstore('mongodb', client=dbjob_b)
    scheduler.add_job(timetest, 'interval', seconds=5)
    scheduler_b.add_job(timetestblock, 'cron', minute=50, hour=13, start_date=datetime.now())
    scheduler.start()
    scheduler_b.start()

1
@scheduler_b.scheduled_job('cron',id='timetest_b',minute=50, hour=13)
def timetestblock():
    print 'i m blocking task'