Skip to main content
 首页 » 编程设计

python之Cron 作业在使用 python 3.7 的 GAE 上 60 秒后以 504 网关超时结束

2024年12月31日10linjiqin

我使用 python 3.7 使用 google app engine 创建了一个新项目,使用 google template 使用 django 创建了一个新项目. 所有 cron 作业都因 504 请求超时而失败。另外,我尝试了新的任务队列 使用谷歌云任务,但这个请求在 60 秒后也失败了。

应用.yaml

# [START django_app] 
runtime: python37 
 
handlers: 
- url: /assets 
  static_dir: assets/ 
- url: /.* 
  script: auto 
 
instance_class: F2 
automatic_scaling: 
  min_idle_instances: 1 
  max_idle_instances: automatic 

定时任务

cron: 
- description: cron eth price 
  url: /cron/ 
  schedule: every 5 mins 

定时 View

class CronView(View): 
    def get(self, request, *args, **kwargs): 
        from time import sleep 
        sleep(240) 
        return HttpResponse('') 

请您参考如下方法:

这只是一个理论:)

文档中似乎有一点冲突。

一方面来自(第二代标准环境)Scheduling Jobs with cron.yaml :

A cron job will invoke a URL, using an HTTP GET request, at a given time of day. An HTTP request invoked by cron can run for up to 60 minutes, but is subject to the same limits as other HTTP requests.

相比之下,对于第一代标准环境,来自Deadlines :

The cron timeout deadline depends on the instance class and scaling type that is configured for your app:

Automatic scaling

Timeout is about 10 minutes.

另一方面,来自 Instance scaling 中的 Deadline 行表(与 the 1st generation 信息一致,但考虑到任务队列支持 is actually different 可能只是文档错误):

Automatic scaling

60-second deadline for HTTP requests, 10-minute deadline for task queue tasks.

...当然,请记住 cron 请求是 HTTP 请求,而不是任务队列任务。