python apscheduler not consistent

2024/5/20 15:08:54

I'm running a scheduler using python apscheduler inside web.py framework. The function runserver is supposed to run everyday at 9 a.m but it is inconsistent. It runs most days but skips a day once in a while.

Code:

import web
from apscheduler.schedulers.blocking import BlockingScheduler #Blocking Scheduler#URLs
urls = ('/startscheduler/','index',)Nightlysched = BlockingScheduler()@Nightlysched.scheduled_job('cron', hour=9)
def runserver():print 2+2 #doing some calculations here#Main function to run the cron JOB
if __name__ == "__main__":Nightlysched.start() #stating the jobapp = web.application(urls, globals())app.run() 

What is the correct way to configure the scheduler to run every day at 9.a.m?

Answer

APScheduler has a grace period during which jobs are allowed to run. If for some reason the scheduler is busy and/or the load of the host is too high, APScheduler might fail to start the job in time.

In this cases, the job will be discarded if it could not be started during the grace time (an explanatory message will be logged if you have initialized Python logging).

Depending on the actual root cause:

  • If the scheduler failed to schedule the job in time, you can use misfire_grace_time=None to tell APScheduler to schedule the job as soon as it can instead of discarding it.
  • By default, only one instance of each job is allowed to be run at the same time. Make sure the previous run has finished. It is possible to set the maximum number of instances for a particular job that the scheduler will let run concurrently, by using the max_instances keyword argument when adding the job. In this case you also may need to use coalesce=False. Do this only if the job takes longer than 24 hours (in your case) and you accept that two instances of your job could be running simultaneously.
  • If there were too many jobs running, but the machine load wasn't too high, it means you have more jobs than what you can run concurrently. You can try increasing the size of the thread pool that APScheduler executor is using to run jobs (this depends on your setup, check: http://apscheduler.readthedocs.org/en/latest/userguide.html).

In summary, I'd first try with misfire_grace_period:

@Nightlysched.scheduled_job('cron', hour=9, misfire_grace_time=None)

As a note, though, as @Alex mentioned I don't grasp why your code works, because the call to Nightlysched.start() should be blocking and preventing your web application from running. I guess this is pasted code and doesn't really represent what you are running. To me, it looks like you should instead be using a non-blocking scheduler like BackgroundScheduler.

https://en.xdnf.cn/q/72977.html

Related Q&A

Change timezone info for multiple datetime columns in pandas

Is there a easy way of converting all timestamp columns in a dataframe to local/any timezone? Not by doing it column by column?

Change permissions via ftp in python

Im using python with ftplib to upload images to a folder on my raspberryPi located in /var/www. Everything is working fine except that uploaded files have 600 permissions and I need 644 for them.Which …

Creating a Persistent Data Object In Django

I have a Python-based maximum entropy classifier. Its large, stored as a Pickle, and takes about a minute to unserialize. Its also not thread safe. However, it runs fast and can classify a sample (a si…

How to catch specific exceptions on sqlalchemy?

I want to catch specific exceptions like UniqueViolation on sqlalchemy.But sqlalchemy throw exceptions only through IntegrityError.So I catched specific exceptions with below code.except sqlalchemy.exc…

numpy.linalg.LinAlgError: SVD did not converge in Linear Least Squares on first run only

I have been wrestling with a known and documented SVD converge issue. Having read up on similar issues raised by others, I have double checked my data and reduced this to a tiny DataFrame - 10 rows/2 c…

Seaborn kde plot plotting probabilities instead of density (histplot without bars)

I have a question about seaborn kdeplot. In histplot one can set up which stats they want to have (counts, frequency, density, probability) and if used with the kde argument, it also applies to the kde…

How can I improve my code for euler 14?

I solved Euler problem 14 but the program I used is very slow. I had a look at what the others did and they all came up with elegant solutions. I tried to understand their code without much success.Her…

Visual Studio Code not recognizing Python import and functions

What do the squiggly lines represent in the image? The actual error the flags up when I hover my mouse over the squiggly line is: Import "pyspark.sql.functions" could not be resolvedPylance …

Pandas interpolate() backwards in dataframe

Going forward, interpolate works great:name days 0 a NaN 1 a NaN 2 a 2 3 a 3 4 a NaN 5 a NaN records.loc[:, days].interpolate(…

PEP8 for long methods name [duplicate]

This question already has an answer here:How to choose proper variable names for long names in python(1 answer)Closed 5 years ago.What is the PEP8 correct way for long methods name? I have a unit test…