Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set limit to the duration of a job with the APScheduler?

I set the scheduler with the "max_instances=10".There can be 10 jobs to run concurrently.Sometimes some jobs blocked, it wsa hanging there.When more than 10 jobs werr blocking there, the exception of "skipped: maximum number of running instances reached(10)". Does APScheduler have a way to set the max time of a job's duration.If the job runs beyond the max time, it will be terminated. If it doesn't have the way, what should I do?

like image 393
hunknownz Avatar asked Nov 17 '15 08:11

hunknownz


People also ask

How does APScheduler work?

In-process task scheduler with Cron-like capabilities Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically. You can add new jobs or remove old ones on the fly as you please.

How do I stop BackgroundScheduler?

In this example, sched is a BackgroundScheduler instance. The main thread runs a while-loop sleeping forever; the scheduler thread triggers the job every 3 seconds. It only stops when you type Ctrl-C from your keyboard or send SIGINT to the process.


1 Answers

APScheduler does not have a way to set the maximum run time of a job. This is mostly due to the fact that the underlying concurrent.futures package that is used for the PoolExecutors do not support such a feature. A subprocess could be killed but lacking the proper API, APScheduler would have to get a specialized executor to support this, not to mention an addition to the job API that allowed for timeouts. This is something to be considered for the next major version.

The question is, what do you want to do with the thread that is still running the job? Since threads cannot be forcibly terminated, the only option would be to let it run its course, but then it will still keep the thread busy.

like image 122
Alex Grönholm Avatar answered Oct 20 '22 01:10

Alex Grönholm