APScheduler provides tasks based on date, fixed time interval and crontab type, and can persist tasks.
APScheduler provides a variety of different schedulers to facilitate developers to use according to their actual needs; At the same time, it also provides different storage mechanisms, which can facilitate collaboration with Redis, database and other third-party external persistence mechanisms. In short, it is very powerful and easy to use.
The main scheduling class of APScheduler
In APScheduler, there are several very important concepts that need to be understood:
Trigger
It contains scheduling logic. Each job has its own trigger to decide which job will run next. It is set according to the time point, frequency, time interval and other parameters defined in the trigger. Triggers are completely stateless except for their own initial configuration.
Job store
Store the scheduled job. The default job storage is to simply save the job in memory, and other job storage is to save the job in the database. The data of a job is serialized when saved in the persistent job store and deserialized when loaded. The scheduler cannot share the same job store. Job store supports mainstream storage mechanisms: redis, mongodb, relational database, memory, etc
Actuator
To handle the operation of jobs, they usually submit the specified callable objects in the job to a thread or enter the city pool. When the job is completed, the executor will notify the scheduler. Pool based operations can make more efficient use of cpu computing resources for different types of job tasks.
Scheduler
Usually, there is only one scheduler in the application, and the scheduler provides the appropriate interface to deal with these. Configuring job storage and executors can be done in the scheduler, such as adding, modifying, and removing jobs.
Here are some common schedulers:
BlockingScheduler: it can only be used within your current process to share computing resources with the current process
BackgroundScheduler: runs scheduling in the background and does not affect the current system computing operation
Asyncoscheduler: if async module is used in the current system, you need to use asynchronous scheduler
GeventScheduler: if gevent is used, this scheduler is required
Tornado scheduler: if tornado is used, the current scheduler is used
Twistedscheduler: the scheduler of twister application
Qtscheduler: scheduler for QT
import time import datetime def job_function(id,iid,uid,user_id,repay_time,month): # Is the current time + expected time greater than the database time times = time.mktime(time.strptime(month, "%Y-%m-%d")) if times-time.time() !=0: months = get_Month(datetime.datetime.strptime(month, '%Y-%m-%d'),'-',1) monthss = time.mktime(time.strptime(months, "%Y-%m-%d")) if time.time() == monthss: # months = # Find the number of people whose type is 0 who have not invested in the investment table according to the bidding id sql = "select * from investment_order where capital_id=%d and type=0"%(int(id)) res = db.find(sql) # Calculate the investment amount + interest rate nums = float(res['money']) * float(res['interest']) # Automatically deduct the amount of the bid issuing user sql1 = "select money,actual_money from user where id=%d"%(int(user_id)) res1 = db.find(sql1) money = float(res1['money']) - float(nums) # Change the amount in the bidding user sql4 = "update user set money=%.2f,actual_money=%.2f where id=%d"%(float(money),float(money),int(user_id)) # Automatically increase bid user amount sql2 = "select money,actual_money from user where id=%d" % (int(uid)) res2 = db.find(sql2) money = float(res2['money']) + float(nums) # Change the amount in the bidding user sql3 = "update user set money=%.2f,actual_money=%.2f where id=%d" % (float(money), float(money), int(uid)) # Query the capital recorded in the investment record table according to the bid form id_ id query whether the status of type is 1 and has been repaid, # try: # If the change is completed, the bidding status will be changed to 1 sql5 = "update investment_order set type=1 where capital_id=%d and user_id=%d and id=%d" % ( int(id), int(uid), int(iid)) db.add(sql4) db.add(sql3) db.add(sql5) # If all records are 1, change the status in the investment table to 8 completed sql6 = "select count(type) as type from investment_order where capital_id=%d and user_id=%d and type=0" % ( int(iid), int(uid)) res6 = db.findall(sql6) for i in res6: if i['type'] == 0: # Modify the status in the investment table to 8 sql7 = "update capital set status=8 where id=%d" % (int(id)) db.add(sql7) else: break db.commit() return jsonify({'code': 200, 'msg': 'Successful repayment'}) # except : # db.rollback() # return jsonify({'code': 403, 'msg': 'repayment failed'}) else: pass else: pass from apscheduler.schedulers.background import BackgroundScheduler def test1(): sched = BackgroundScheduler() # Query information for repayment sql = "select c.id,c.starttime,c.repay_time,c.user_id,i.user_id as uid,i.id as iid from capital as c inner join investment_order as i on c.id=i.capital_id where c.status=7" res = db.findall(sql) if res: # Add scheduling task for i in res: month = get_Month(i['starttime'], '+', i['repay_time']) # The scheduling method is timedTask, the trigger selects interval, and the interval length is 2 seconds # sched.add_job(job_function, 'interval', seconds=2, args=[i['id'],i['iid'],i['uid'],i['user_id'],i['repay_time'],month]) # sched.add_job(job_function, 'cron',month=int(i['repay_time'])-1, hour=0, minute=30,args=[i['id'],j['iid'],j['uid'],i['user_id'],month]) # Every day at 00:30 sched.add_job(job_function, 'cron', day_of_week='*', hour=0, minute=30,args=[i['id'],i['iid'],i['uid'],i['user_id'],i['repay_time'],month]) # Start scheduling task sched.start() return 'ok' else: pass