Skip to main content
 首页 » 编程设计

python之如何使用 Google Task Queues API 安排任务

2025年05月04日17mayingbao

我想安排一个任务(即代码块)在稍后执行,其中执行的时间和日期以及其他参数作为任务负载传入。

所以我们有一个基于 Python (Django) 的 Appengine 应用程序,其中:

任务在tasks.py

中定义
@csrf_exempt 
def task_myfunction(request): 
    if request.method == 'POST': 
      # Read POST parameters 
      # Perform task 
    return HttpResponse("Success") 

有效载荷被添加到 views.py 中的队列中,有效载荷被 POST 到提供的 url,该 url 映射到上面定义的函数。

from google.appengine.api import taskqueue 
 
# ..Somewhere inside a view.. 
taskqueue.add(queue_name='myqueue', url='/task/myfunction', params={ 
    # Specify parameters 
    }) 

有没有办法,我可以指定执行的确切时间(带时区)以及负载数据,以便任务自动从队列中提取并在那个时刻执行。

请您参考如下方法:

您可以计算所需时间,然后在添加任务时设置倒计时 或eta。

倒计时:

Minimum time to wait before executing this task, in seconds. Defaults to zero. Do not specify a countdown if you specified an eta.

时间:

Earliest time of task execution, in seconds. It is a datetime.datetime specifying the absolute ETA or None; this may be timezone-aware or timezone-naive. If None, defaults to now. It must be less than 30 days from current date.

https://developers.google.com/appengine/docs/python/taskqueue/tasks

正如您在上面的页面中看到的那样,似乎没有一种方法可以完全按照您的意愿进行操作:时区和特定时间。我认为你必须做那些计算。