使用Channel API从任务队列中的任务接收消息

时间:2013-04-29 02:22:23

标签: javascript google-app-engine python-2.7 channel channel-api

我正在尝试找出Task Queue和Channel API。

当用户提交表单时,我有一个预计会在任务队列中运行> 60秒的进程。然后,此过程应在执行时通过通道api发送消息。粗糙我似乎无法使这项工作。任何人都可以告诉我为什么SendMessagesHandler无法向客户端发送消息javascript?或者也许客户端javascript没有正确收到?此外,我在日志控制台中看不到来自logging的{​​{1}}消息,它只显示了工厂SendMessagesHandlerINFO ... GET的运行情况。在我写这篇文章时,我意识到我必须在这里遇到一个以上的问题,任务确实在任务队列中创建,我可以在SDK控制台中看到它,但它似乎只是继续无休止地运行,我希望它运行大约1m2sec。如果我从POST中取出sleep命令,它会快速运行,甚至可以在SDK控制台中捕获它。

提前感谢您的帮助。

这是main.py文件:

SendMessageHandler

这是template.html

#!/usr/bin/env python
import webapp2
import os
import jinja2
import logging
from time import sleep
from google.appengine.api import taskqueue, users, channel
from google.appengine.ext import db

class MainHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            key = self.request.get('key')
            if not key:
                key = user.user_id()
            key_link = 'http://localhost:8080/?key=' + key
            token = channel.create_channel(key)
            template_values = {'token': token,
                    'me': user.user_id(),
                    'key': key,
                    'key_link': key_link,
                    'initial_message': 'Nothing to show yet'}
            template = jinja_environment.get_template('template.html')
            self.response.out.write(template.render(template_values))
        else:
            self.redirect(users.create_login_url(self.request.uri))

    def post(self):
        key = self.request.get('key')
        taskqueue.add(url='/sendmessages', params={'key': key})
        self.redirect('/')

class SendMessagesHandler(webapp2.RequestHandler):
    def post(self):
        key = self.request.get('key')
        sleep(2)
        channel.send_message(key, 'Starting to send messages...')
        logging.info('Starting to send messages...')
        i = 0
        while i < 60:
            i += 1
            logging.info('Counter incremented.')
            channel.send_message(key, 'Counter incemented.')
            sleep(1)

jinja_environment = jinja2.Environment(
        loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
app = webapp2.WSGIApplication([
        ('/', MainHandler),
        ('/sendmessages', SendMessagesHandler)
    ], debug=True)

以下是浏览器的视图来源:

<html>
    <head>
        <title>Simple Task Queue Channel API</title>
        <script src='/_ah/channel/jsapi'></script>
    </head>
    <body>
        <script type='text/jacascript'>
            onOpened = function() {};
            onMessage = function(message) {
                var messageBox = document.getElementById("messageBox");
                messageBox.innerHTML = message;
            };
            onError = function() {};
            onClose = function() {};
            channel = new goog.appengine.Channel('{{token}}');
            socket = channel.open();
            socket.onopen = onOpened;
            socket.onmessage = onMessage;
            socket.onerror = onError;
            socket.onclose = onClose;
        </script>
        <form method="post" action="/">
            <input type="text" name="key" value="{{ key }}">
            <input type="submit" name="submit" value="Receive Messages">
        </form>
        <div id="messageBox"><!-- message should go in here --></div>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

我发现至少两个错误:

第一

<script type='text/javascript'>

第二

onMessage = function(message) {
        var messageBox = document.getElementById("messageBox");
        messageBox.innerHTML = message.data;
    };