I have been using apscheduler
. A recurring problem regarding the package is that if for any reason, a running job hangs indefinitely (for example if you create an infinite while loop inside of it) it will stop the whole process forever as there is no time limit option for the added jobs.
Apscheduler has stated multiple times that they will not add a timelimit due to various reasons (short explanation here), however the problem still remains. You could create a job that will run for days, only to stop because a webrequest gets no response and apscheduler will wait for it indefinitely.
I've been trying to find a way to add this time limit to a job. For example using the wrapt-timeout-decorator
package. I would create a function which runs my job inside it, that has a time limit, and I add this function to aposcheduler. Unfortunately, the two packages collide with a circular import.
from wrapt_timeout_decorator.wrapt_timeout_decorator import timeout
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
class MyJob: # implementation is unnecessary to show here
...
@timeout(dec_timeout=600, use_signals=False)
def run_job(job: MyJob) -> None:
job.run()
job = MyJob()
scheduler = BackgroundScheduler(daemon=True)
scheduler.add_job(func=run_job, kwargs={"job": job}, trigger=CronTrigger.from_crontab(sheet_job.cron))
scheduler.start()
File "C:\Users...\AppData\Local\Programs\Python\Python39\lib\site-packages\multiprocess\context.py", line 62, in Pipe from .connection import Pipe ImportError: cannot import name 'Pipe' from partially initialized module 'multiprocess.connection' (most likely due to a circular import) (C:\Users...\AppData\Local\Programs\Python\Python39\lib\site-packages\multiprocess\connection.py)
I've also tried adding a self made timeout decorator, shown here, but I did not get the desired outcome.
My question is: Is there a way to add a time limit to an apscheduler job, or are there any other similar packages where creating a cron job with a time limit is possible, or do you know of any self-made solution? (the program will run on Windows).