简体   繁体   English

Flask应用程序中的单个apscheduler实例

[英]Single apscheduler instance in Flask application

Setup: 建立:

  • Flask application running in Apache's httpd via wsgi Flask应用程序通过wsgi在Apache的httpd中运行
  • Single wsgi process with 25 threads: WSGIDaemonProcess myapp threads=25 单个wsgi进程有25个线程: WSGIDaemonProcess myapp threads=25
  • apscheduler to run jobs (send emails) apscheduler来运行工作(发送电子邮件)
  • RethinkDB as the backend for the job store RethinkDB作为工作存储的后端

I'm trying to prevent apscheduler from running the same job multiple times by preventing multiple instances of apscheduler from starting. 我试图通过防止apscheduler的多个实例启动来阻止apscheduler多次运行相同的作业。 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. 从pid可以看出,有两个进程正在某处/以某种方式启动。 How can I prevent this? 我怎么能阻止这个? Where is this configuration in httpd? httpd中的这个配置在哪里?

Say I did want two processes running, I could use flock to prevent apscheduler from starting twice. 假设我确实想要运行两个进程,我可以使用flock来防止apscheduler重启两次。 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. 但是,这不起作用,因为不启动apscheduler的进程将无法添加/删除作业,因为app.config['SCHEDULER']设置为该进程使用。

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? 使用多个进程配置/设置Flask Web应用程序的最佳方法是什么,这些进程可以添加/删除作业,但却阻止调度程序多次运行作业?

I finally settled on using a file-based lock to ensure that the task doesn't run twice: 我最终决定使用基于文件的锁来确保任务不会运行两次:

def get_lock(name):
    fd = open('/tmp/' + name, 'w')

    try:
        flock(fd, LOCK_EX | LOCK_NB)  # open for exclusive locking
        return fd
    except IOError as e:
        logger.warn('Could not get the lock for ' + str(name))
        fd.close()
        return None


def release_lock(fd):
    sleep(2)  # extend the time a bit longer in the hopes that it blocks the other proc
    flock(fd, LOCK_UN)
    fd.close()

It's a bit of a hack, but seems to be working... 这有点像黑客,但似乎工作......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM