调用多次调用单个EC2实例上的APScheduler [英] APScheduler on a single EC2 instance getting called multiple times

查看:311
本文介绍了调用多次调用单个EC2实例上的APScheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部署在单个AWS EC2实例上的Flask应用程序。在我的__init__.py文件中,我已经实例化了一个BackgroundScheduler,计划每隔1小时运行一次作业。
这里是我的__init__.py代码的一个例子:
$ b $ pre $ $ $ $ $ $ application $ Flags(__ name__)
app =应用程序
scheduler = BackgroundScheduler()
run_my_jobs = scheduler.add_job(my_job,'interval',hours = 1)
scheduler.start()

我假设既然实例化是在Flask上下文之外完成的,并且在EC2上只运行一个实例,那么我的调度器只应该被实例化曾经,无论有多少用户连接到我的Flask应用程序全天。



过去几个月一般情况下是这样,但最近在过去日子我注意到调度器一直在执行这个工作每小时几乎2-3次。虽然我一直在继续推动代码生产,__init__.py文件保持不变,所以我很困惑,可能的原因是什么导致调度程序的多个实例化?




  1. 使用多个worker process

  2. 将代码放在应用程序入口点的模块中,而不是使用保护它,如果__name__ =='__main __': block

从你的描述来看,我认为#1不是这样,所以我会去#2。


I have a Flask application that's deployed on a single AWS EC2 instance. In my __init__.py file I've instantiated a BackgroundScheduler with a job scheduled to run at every 1 hour interval. Here's an example of my __init__.py code:

application = Flask(__name__)
app = application
scheduler = BackgroundScheduler()
run_my_jobs = scheduler.add_job(my_job,  'interval', hours=1)
scheduler.start()

I would assume that since the instantiation is done outside of the Flask context, and with only one single instance running on EC2, that my scheduler should only be instantiated once, regardless of how many users are connected to my Flask app throughout the day.

That has generally been the case for the past couple months, however recently in the past couple days I noticed the scheduler has been executing the job almost 2-3 times per hour. While I've been continuing to push code to production, the __init__.py file has remained unchanged so I'm confused as to what are possible reasons to cause multiple instantiations of the scheduler?

解决方案

There are generally two ways this can happen:

  1. Using more than one worker process
  2. Putting the code in the module that is your application's entry point and not protecting it with an if __name__ == '__main__': block

From your description I assume #1 is not the case, so I'd go for #2.

这篇关于调用多次调用单个EC2实例上的APScheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆