Category Archives: apscheduler

Single apscheduler instance in Flask application

Setup:

  • Flask application running in Apache's httpd via wsgi
  • Single wsgi process with 25 threads: WSGIDaemonProcess myapp threads=25
  • apscheduler to run jobs (send emails)
  • RethinkDB as the backend for the job store

I'm trying to prevent apscheduler from running the same job multiple times by preventing multiple instances of apscheduler from starting. Currently I'm using the following code to ensure the scheduler is only started once:

    if 'SCHEDULER' not in app.config or app.config['SCHEDULER'] is None:
        logger.info("Configuring scheduler")
        app.config['SCHEDULER'] = scheduler.configure()

However, when I look at my logs, I see the scheduler being started twice:

[07:07:56.796001 pid 24778 INFO] main.py 57:Configuring scheduler
[07:07:56.807977 pid 24778 INFO] base.py 132:Scheduler started
[07:07:56.812253 pid 24778 DEBUG] base.py 795:Looking for jobs to run
[07:07:56.818019 pid 24778 DEBUG] base.py 840:Next wakeup is due at-10-14 11:30:00+00:00 (in 1323.187678 seconds)
[07:07:57.919869 pid 24777 INFO] main.py 57:Configuring scheduler
[07:07:57.930654 pid 24777 INFO] base.py 132:Scheduler started
[07:07:57.935212 pid 24777 DEBUG] base.py 795:Looking for jobs to run
[07:07:57.939795 pid 24777 DEBUG] base.py 840:Next wakeup is due at-10-14 11:30:00+00:00 (in 1322.064753 seconds)

As can be seen by the pid, there are two processes that are being started somewhere/somehow. How can I prevent this? Where is this configuration in httpd?

Say I did want two processes running, I could use flock to prevent apscheduler from starting twice. However, this won't work because the process that does NOT start apscheduler won't be able to add/remove jobs because app.config['SCHEDULER'] set for that process to use.

What is the best way to configure/setup a Flask web app with multiple processes that can add/remove jobs, and yet prevent the scheduler from running the job multiple times?

Send emails in background from WSGI Bottle App

I have a REST API using the bottle.py framework in apache using mod_wsgi. I need to send out emails when certain actions occur but that process is slowing down the server API response, often to the point of timeout.

I've looked into things like APScheduler but all the examples are for Flask and I'm too deep into using Bottle to switch at the moment, so that's not an option.

Is there a good way without doing cron jobs and such to send emails on a background task via bottle.py?