我正在尝试找出任务队列和通道 API。
当用户提交表单时,我有一个预计将运行 >60 秒的进程添加到任务队列中。然后,此过程应在执行时通过通道 api 发送消息。粗鲁的我似乎无法完成这项工作。谁能告诉我为什么SendMessagesHandler
无法向客户端javascript发送消息?或者客户端javascript没有正确接收?此外,我没有在日志控制台中看到logging
消息SendMessagesHandler
,它只显示磨机的运行INFO ... GET
或POST
. 当我写这篇文章时,我意识到我必须有不止一个问题在这里发生,任务确实在任务队列中创建,我可以在 SDK 控制台中看到它,但它似乎只是继续无休止地运行,我希望它运行大约 1 平方米秒。如果我拿出sleep
它的命令SendMessageHandler
运行得很快,甚至可以在 SDK 控制台中捕获它。
在此先感谢您的帮助。
这是 main.py 文件:
#!/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)
这是template.html
<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>
这是来自浏览器的视图源:
<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('channel-2840733866-1367208241-185804764220139124118');
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="185804764220139124118">
<input type="submit" name="submit" value="Receive Messages">
</form>
<div id="messageBox"><!-- message should go in here --></div>
</body>
</html>