【问题标题】:python flask scheduling: all workers start scheduler taskpython烧瓶调度:所有工作人员启动调度程序任务
【发布时间】:2019-03-28 20:54:41
【问题描述】:

我在烧瓶应用程序中使用计划任务:

    import time
    import atexit

    from apscheduler.schedulers.background import BackgroundScheduler


    def intervall():
        with app.app_context():
            call_function_to_do()

    scheduler = BackgroundScheduler()
    scheduler.add_job(func=intervall, trigger="interval", seconds=5)
    scheduler.start()

    # Shut down the scheduler when exiting the app
    atexit.register(lambda: scheduler.shutdown())

我的问题是所有 gunicorn 工作人员都在启动调度程序任务。在这种情况下,检查超时情况并向用户发送电子邮件。

有人知道如何只让一名工人执行 call_function_to_do() 吗?

【问题讨论】:

    标签: python flask apscheduler


    【解决方案1】:

    基本上有两种选择。 FAQ 中概述的其中之一是将调度程序分成单独的进程并使用某些远程访问协议(例如使用 RPyC)与进程通信。另一种方法是在 Flask 应用程序中启动调度程序,但要确保只运行一个这样的实例。您可以使用某种锁定机制,例如RedLock.

    【讨论】:

      猜你喜欢
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 2019-04-22
      相关资源
      最近更新 更多