Search code examples
pythongoogle-app-enginetask-queue

Simple Google App Engine Task Queue tutorial? Flask/ Python/ GAE


I can't even get a task queue to fire a simple print statement.

I don't know if my app structure is incorrect or not...

What's the most simple example of a task queue and the structure for it?

Because this isn't working for me:

@app.route('/task', methods=["GET", "POST"])
def testtask():
  print "Hi, I'm calling the task"
  taskqueue.add(
    url='/execute_task'
  )
  return "I called the task"

@app.route('/execute_task', methods=["GET", "POST"])
def execute_task():
  print "I'm executing the task"
  return "I excecuted the task"

both routes are in the app.yaml file:

- url: /execute_task
  script: app.app
  login: admin
- url: /task
  script: app.app
  login: admin

in terminal:

Hi, I'm calling the task INFO 2018-01-28 23:00:49,521 module.py:788] default: "GET /task HTTP/1.1" 200 17

http://localhost:8000/taskqueue

shows:

Queue Name Maximum Rate Bucket Size Oldest Task (UTC) Tasks in Queue

default 5.00/s 5 2018/01/28 22:59:59(0:04:44 ago) 2


Solution

  • A print statement shown to a task is meaningless. You'll never see it. Only the task sees it. Let the task talk to you via the logs.

    See if this makes better sense:

    from flask import Response
    from google.appengine.api import taskqueue
    import logging
    
    @app.route('/task', methods=["GET", "POST"])
    def testtask():
      logging.info("sending task to queue in testtask...")
      taskqueue.add(
        url    = "/execute_task",
        name   = "task_name_here",
        method = "POST",
        params = {
          "sender": "me"
        }
      )
      return Response("sending task to queue... Check logs", mimetype='text/plain', status=200)
    
    @app.route('/execute_task', methods=["GET", "POST"])
    def execute_task():
      logging.info("I did the task")
      logging.info("params: {}".format(request.form))
      return Response("did it. You'll never see this message", mimetype='text/plain', status=200)