Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: cannot schedule new futures after interpreter shutdown when the job is triggered #918

Closed
3 tasks done
Anzal-calixto opened this issue May 20, 2024 · 6 comments
Labels

Comments

@Anzal-calixto
Copy link

Anzal-calixto commented May 20, 2024

Things to check first

  • I have checked that my issue does not already have a solution in the FAQ

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Version

3.10.4

What happened?

the expected behaviour is to run the scheduled job.

But what happend was .

Error submitting job "job (trigger: cron[day_of_week='mon', hour='16', minute='4', second='0'], next run at: 2024-05-20 16:04:00 IST)" to executor "default"
Traceback (most recent call last):
  File "C:\Users\rohit\AppData\Local\Programs\Python\Python39\lib\site-packages\apscheduler\schedulers\base.py", line 988, in _process_jobs
    executor.submit_job(job, run_times)
  File "C:\Users\rohit\AppData\Local\Programs\Python\Python39\lib\site-packages\apscheduler\executors\base.py", line 71, in submit_job
    self._do_submit_job(job, run_times)
  File "C:\Users\rohit\AppData\Local\Programs\Python\Python39\lib\site-packages\apscheduler\executors\pool.py", line 28, in _do_submit_job
    f = self._pool.submit(run_job, job, job._jobstore_alias, run_times, self._logger.name)
  File "C:\Users\rohit\AppData\Local\Programs\Python\Python39\lib\concurrent\futures\thread.py", line 163, in submit
    raise RuntimeError('cannot schedule new futures after '
RuntimeError: cannot schedule new futures after interpreter shutdown

How can we reproduce the bug?

To reproduce the bug you can try this code which i was using

from threading import Thread
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.executors.pool import ThreadPoolExecutor
from apscheduler.triggers.cron import CronTrigger
from pytz import utc
from time import sleep



jobstores_1 = {'default': SQLAlchemyJobStore(
    url='sqlite:///jobs.db')}
executors_1 = {'default': ThreadPoolExecutor(20)}
job_defaults_1 = {'max_instances': 20}

scheduler_1 = BackgroundScheduler(
    jobstores=jobstores_1, executors=executors_1, job_defaults=job_defaults_1, timezone=utc)

scheduler_1.start()

def thread_1():
    while True:
        print("thread 1\n")
        sleep(5)

def thread_2():
    while True:
        print("thread 2\n")
        sleep(5)

def thread_3():
    while True:
        print("thread 3\n")
        sleep(5)

def thread_4():
    while True:
        print("thread 4\n")
        sleep(5)

def create_scheduled_job(job_func, day_of_week, hour, minute, second,scheduler, job_id):
    # Create a new BackgroundScheduler instance if jobstores, executors, or job_defaults are not provided
    


    # Create a cron trigger for each selected day of the week
    for day in day_of_week:
        scheduler.add_job(
            job_func,
            trigger=CronTrigger(day_of_week=day, hour=hour, minute=minute, second=second),
            id=f"{job_id}_{day}"  # Append the day to the job ID for uniqueness

        )

def job():
    for i in range(2):
        print(f"job{i}")

def main():

    thread_a = Thread(target= thread_1)
    thread_b = Thread(target= thread_2)
    thread_c = Thread(target= thread_3)
    thread_d = Thread(target= thread_4)



    thread_a.start()
    thread_b.start()
    thread_c.start()
    thread_d.start()
    create_scheduled_job(
        job,
        day_of_week= ["mon"], 
        hour=16,
        minute=10,
        second=0,
        scheduler=scheduler_1,
        job_id='job2',
    )


if __name__ == "__main__":
    main()
@agronholm
Copy link
Owner

So, how long do I need to run this script to reproduce the issue? From the looks of it, it only fires at 16:10 every Monday. Would an IntervalTrigger not reproduce it?

@agronholm
Copy link
Owner

Never mind, I was able to repro it after adjusting the time so it fires the job really soon.

@agronholm
Copy link
Owner

Looks like this can be reproed with just the default memory job store too.

@agronholm
Copy link
Owner

Ahhhh....I see the problem. The interpreter really DID shut down. I asked you about this and you denied it. But the script runs to completion and the only thing preventing it from shutting down are the non-daemonic threads you spawned. So to fix this, don't let the main thread run to completion. Use BlockingScheduler instead of BackgroundScheduler if you're not doing anything else in the main thread.

@agronholm agronholm closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
@agronholm
Copy link
Owner

@Anzal-calixto
Copy link
Author

i didnt know if the interpreter was shutting down or not. sorry for not replying soon i live in india. i will try to run as you suggested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants