title: Single apscheduler instance in Flask application
body: <p>Setup:</p>
  
  <ul>
  <li>Flask application running in Apache's httpd via wsgi</li>
  <li>Single wsgi process with 25 threads: <code>WSGIDaemonProcess myapp threads=25</code></li>
  <li>apscheduler to run jobs (send emails)</li>
  <li>RethinkDB as the backend for the job store</li>
  </ul>
  
  <p>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:</p>
  
  <pre><code>    if 'SCHEDULER' not in app.config or app.config['SCHEDULER'] is None:
          logger.info("Configuring scheduler")
          app.config['SCHEDULER'] = scheduler.configure()
  </code></pre>
  
  <p>However, when I look at my logs, I see the scheduler being started twice:</p>
  
  <pre><code>[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)
  </code></pre>
  
  <p>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?</p>
  
  <p>Say I did want two processes running, I could use <code>flock</code> 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 <code>app.config['SCHEDULER']</code> set for that process to use.</p>
  
  <p>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?</p>