【问题标题】:How to keep a python server alive for running scheduled tasks on threads?如何让 python 服务器保持活动状态以在线程上运行计划任务?
【发布时间】:2019-09-26 22:21:09
【问题描述】:

我用 Python 编写了一个小型后端应用程序(在 Google App Engine 上运行),作为我在不同应用程序中使用的几个 Google Cloud Functions 的自定义远程调度程序。通常,函数会要求后端使用给定的有效负载安排回调。一切都很好,除了在后端应用程序上,负责回调我的 Cloud Functions 的线程通常会在几个小时后死掉:https://imgur.com/a/8ulENjf

不用说我不是joining线程。

我已经阅读了 UNIX 服务器使用 Python 发出的 Python SIGINT 信号,但我需要先排除更简单的选项。相关部分代码如下:

import time 
from datetime import datetime, timedelta
import sched
from threading import Thread

scheduler = sched.scheduler(time.time, time.sleep)
scheduler.enterabs(schedTime, 1, callback, kwargs=payload)
t.thread = Thread(target=scheduler.run) #dispatching scheduler main function to thread
t.start()

【问题讨论】:

    标签: python-3.x google-app-engine google-cloud-platform job-scheduling


    【解决方案1】:

    我会说该实例由于多种原因而被杀死,这些原因在here 中列出,因此很可能在几个小时后满足其中一个陈述,我猜这两个:

    • 您的应用程序已用完 Instance Hours 配额。
    • 实例超出了为其配置的 instance_class 的最大内存。

    您还应该考虑到 App Engine 会尝试让您的实例无限期地运行,但在某些时候实例会被终止并重新启动。

    您可以通过在 App Engine app.yaml 文件中使用 scaling elements 来控制 App Engine 应用程序的缩放。例如,您可以将 max_instances 和 min_instances 设置为 1,以确保始终有一个实例在运行。或者使用手动缩放来设置静态实例数量。

    【讨论】:

    • 非常感谢您的详细解答。但是,我需要评估您是否正确,因为您的假设与我的代码无关,这对这种不良行为负责。将根据您的建议工作并让您知道:)
    • 好的,我的问题解决了。由于我对代码进行了实质性更改,因此我无法完全确定修复它的原因,但我认为您的输入帮助很大,特别是注册了几个永久实例(我所做的是使用队列和生成器给我的线程带来一些纪律)。所以非常感谢!
    猜你喜欢
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多