- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在尝试了解 Google 应用引擎 (gae) 的任务队列。
我有一个与 this 非常相似的问题
因此我根据“chachan”的解决方案编写了一个小应用程序。
所以我的主要问题是如何从 html 站点轮询进度值。
我编写了一个小型的“Hello gae 任务队列”应用程序,它从用户那里获取一个号码并打印该数字与 1 到 10 之间所有数字的乘积。计算由任务队列完成。来源如下:
#hellogaequeue.py
import os
import time
import webapp2
import jinja2
import logging
import json
from google.appengine.api import taskqueue
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
JINJA_ENV = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)
myresults = [] # the results of the task in a list
myProgressValue = 0 # represents the progess of the task. range[ 0, 100 ]
#define the main handler to show the html-site and post some data to the task.
class MainHandler(webapp2.RequestHandler):
def get(self):
#gets the calculated results of the task (therefor empty at first call)
#and give it to the jinja2 framework to render the html page
template_values = {'myresults': myresults}
counter_template = JINJA_ENV.get_template('counter.html')
self.response.out.write(counter_template.render(template_values))
# if user clicks the button, we write the data from the input field onto 'key' and
# than add a new task, with the parameter holding the value of 'key'
def post(self):
key = self.request.get('key')
# Add the task to the default queue.
taskqueue.add(url='/worker', params={'key': key})
self.redirect('/') #calls MainHandler.get(...), which shows than 'counter.html' again
#define the handler for the task worker
class TaskWorker(webapp2.RequestHandler):
def post(self):
# ensure to use the global variables
global myresults
global myProgressValue
# get the 'params={'key': key}' value
key = self.request.get('key')
#now imagine this for loop takes 5 minutes to finish
#how to show the progress of this loop in 'counter.html'
for x in xrange(1, 11):
time.sleep(1)
res = x*int(key)
myresults.append(str(res))
myProgressValue = myProgressValue + 10
class ProgressWorker(webapp2.RequestHandler):
def get(self):
global myProgressValue
logging.info("Call to ProgressHandler. MyProgressValue is = {p}".format(p=myProgressValue))
json_result = json.dumps({ "progress" : myProgressValue } )
self.response.headers['Content-Type'] = 'application/json; charset=UTF-8'
self.response.out.write(json_result)
application = webapp2.WSGIApplication( [ ('/', MainHandler), ('/worker', TaskWorker), ('/progress', ProgressWorker) ], debug=True)
“counter.html”的来源是:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello gae task queue with jQuery UI Progressbar</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
</head>
<body>
<form action="/" method="POST">
<input type="text" name="key" id="key">
<input type="submit" value="start gae task">
</form>
{% for counter in myresults %}
<li>
{{counter}}
</li>
{% endfor %}
<div id="progressbar"></div>
<div id="dbg"></div>
<script>
function refresh()
{
var progressVal = 0
$.ajax(
{
type: 'GET',
url: '/progress',
success: function(data)
{
var obj = JSON.parse(data);
progressVal = obj.progress;
document.getElementById("dbg").innerHTML = obj.progress;
},
error: function(xhr, status, error)
{
alert(xhr.status);
alert(error);
}
});
$( "#progressbar" ).progressbar(
{
value: progressVal
});
}
$(function()
{
setInterval("refresh()", 3000);
});
</script>
</body>
</html>
所以,第一个问题是:
为什么是jquerys progressbar不工作?(没有警报。)我做错了什么?
第二个问题是:
这是正确的方法吗?
考虑到for循环花费的时间超过60秒。因此不可能将其放入 MainHandler.post(..) 函数中,因为这会引发 Deadline超过错误。我还应该提到 for 循环无法并发运行。
额外信息:我的 gae 项目的文件夹结构是:
hellogaequeue
-templates
-counter.html
-app.yaml
-hellogaequeue.py
-queue.yaml
app.yaml 看起来像:
application: queuetest-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: jinja2
version: latest
handlers:
- url: /.*
script: hellogaequeue.application
最佳答案
我终于成功了。
1.) 问题是引用的 Jquery 库似乎无法在进度条上正常工作。(请参阅this以获得更好的解释)
所以这是“hello 任务队列与 jquery 进度条”应用程序的最终来源:
import os
import time
import webapp2
import jinja2
import json
from google.appengine.api import taskqueue
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
JINJA_ENV = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)
myresults = []
myProgressValue = 0 #range[ 0, 100 ]
class MainHandler(webapp2.RequestHandler):
def get(self):
template_values = {'myresults': myresults, 'progressScriptActive':False}
counter_template = JINJA_ENV.get_template('counter.html')
self.response.out.write(counter_template.render(template_values))
def post(self):
key = self.request.get('key')
# Add the task to the default queue.
taskqueue.add(url='/worker', params={'key': key})
template_values = {'myresults': myresults, 'progressScriptActive':True}
counter_template = JINJA_ENV.get_template('counter.html')
self.response.out.write(counter_template.render(template_values))
class TaskWorker(webapp2.RequestHandler):
def post(self):
global myresults
global myProgressValue
key = self.request.get('key')
for x in xrange(1, 11):
time.sleep(1)
res = x*int(key)
myresults.append(str(res))
myProgressValue = myProgressValue + 10
class ProgressWorker(webapp2.RequestHandler):
def get(self):
global myProgressValue
json_result = json.dumps( myProgressValue )
self.response.headers['Content-Type'] = 'application/json; charset=UTF-8'
self.response.out.write(json_result)
application = webapp2.WSGIApplication(
[
('/', MainHandler),
('/worker', TaskWorker),
('/progress', ProgressWorker)
], debug=True)
和 HTML jinja2 模板(“counter.html”):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello gae task queue with jQuery UI Progressbar</title>
<script src="/static/external/jquery/jquery.js"></script>
<script src="/static/jquery-ui.js"></script>
<link href="/static/jquery-ui.css" rel="stylesheet">
</head>
<body>
<form action="/" method="POST">
<input type="text" name="key" id="key">
<input type="submit" value="start gae task">
</form>
{% for counter in myresults %}
<li>
{{counter}}
</li>
{% endfor %}
<div id="progressbar"></div>
<div id="dbg"></div>
<script>
var intervalHandler = 0;
function refresh()
{
$.ajax(
{
type: 'GET',
url: '/progress',
success: function(data)
{
var progressVal = $.parseJSON( data );
document.getElementById("dbg").innerHTML = progressVal;
if( progressVal > 99 )
{
$( "#progressbar" ).progressbar( { value: 100 });
alert("Finish");
clearInterval(intervalHandler); // stop the interval
intervalHandler = null;
//window.location.reload(true);// will perform a post request, but we need a GET request
var loc = window.location;
window.location = loc.protocol + '//' + loc.host + loc.pathname + loc.search;
}
else
{
$( "#progressbar" ).progressbar( { value: progressVal });
}
},
error: function(jqXHR, textStatus, errorThrown)
{
alert("Request Error !!!!");
}
});
};
{% if progressScriptActive %}
$(function()
{
intervalHandler = setInterval("refresh()", 2000);
});
{% endif %}
</script>
</body>
</html>
您必须引用从http://jqueryui.com/download/下载的jquery文件具有选定的 UI 主题(没有您将看不到任何内容!)
下载 zip 并在“hellogaequeue.py”文件中引用这些解压缩的 jquery 源代码。您的文件夹结构应如下所示:
hellogaequeue
-templates
-counter.html
-static
- jquery-ui.css
- jquery-ui.js
- ... and all the other unziped files/folders
-app.yaml
-hellogaequeue.py
-queue.yaml
对于 app.yaml,您必须添加:
handlers:
- url: /static
static_dir: static
我知道这不是一个干净的解决方案,因为“progressScriptActive”技巧(仅在 post-request 之后启动轮询间隔)。因此,如果有人有更干净的解决方案,我会很高兴看到它。
关于jquery - 如何显示正在运行的谷歌应用程序引擎任务队列任务的进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26224484/
以下代码: if (!(ep = engOpen("\0"))) { fprintf(stderr, "\nCan't start MATLAB engine\n");
我在谈论一些网络事物,例如 http://uservoice.com/ 你能推荐任何其他类似的服务、网站,或者可能是(甚至更好)一个现成的引擎来部署在自己的服务器上? 实际上,更多关于系统的问题,可以
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在寻找一个矩阵表达式解析器/引擎。例如, 3 * A + B * C 其中 A、B、C 是矩阵是一个典型的表达式。这应该类似于(单值)数学表达式解析器/引擎,但应该处理矩阵值和变量。我已经用谷歌搜
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this qu
是否有基于 .net 的 cometd 引擎?比如 Ajax 推送引擎 那是免费和开源的吗? 最佳答案 轨道式 Orbited是一个 HTTP 守护进程,针对长期 cometd 连接进行了优化。它旨在
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在寻找支持以下功能的 haml javascript“端口”: 存储在文件中的模板。 JSON 输入。 支持“集合”[{Booking},{Booking},{Booking}] 进行迭代处理。
我在 IronPython 中托管 IronPython。我没有找到使用等效的命令行参数初始化它的方法:-X:FullFrames . 我的代码有点像这样: import clr clr.AddRef
我想将我工作的公司的所有松散信息整合到一个知识库中。 Wiki 似乎是一种可行的方法,但大部分相关信息都隐藏在 PST 文件中,并且需要很长时间才能说服人们将他们的电子邮件(包括附件)手动翻译成 Wi
我已经使用缓存的 flutter 引擎 flutter 到现有的 native 应用程序(添加到应用程序)中。 override fun onCreate(savedInstanceState: Bu
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我正在使用 Django Cassandra我已经定义了我的模型,我可以用它来命名一个表: class Meta: db_table = "table_name" 但是,Cassand
类似于 NoSQL 数据库,但适用于 OLAP。当然是开源的:) 编辑: OLAP 引擎在幕后使用关系数据库。例如 SAPBW 可以使用 Oracle 等。我的意思是一个没有这个底层关系数据库的 OL
我正在使用以下片段来 enable Razor templating in my solution (在 ASP.NET MVC3 之外)。是否可以轻松实现布局? 背景资料: 我在这一点上(模板编译成
我们目前使用闭源知识库解决方案,所见即所得创建文章是TinyMCE(看起来可能是修改/简化的)。 他们目前根本不允许更改它(添加插件等,除非您可以以某种方式注入(inject)插件)。 我确实拥有对
我正在评估我们的高性能电信应用程序的 BPEL 引擎,但性能似乎很差。我们评估了 Apache Ode、SunBPEL 引擎、Active BPEL 等。您知道任何更快的 BPEL 引擎实现或 C/C
Elastic / Lucene真的需要在文档中存储所有索引数据吗?您难道不就通过通过传递数据,以便Lucene may index the words into its hash table并为每个
我是 3D 游戏新手?我正在使用 Libgdx。如何计算像 Tetromino Revolution 游戏这样的透视相机的参数?请给我任何想法。 看图片:http://www.terminalstud
我是一名优秀的程序员,十分优秀!