- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
问题:python webapp2 + jQuery Ajax 在接收大文本数据响应方面表现极其糟糕(在 1.7MB 的有效负载往返中需要超过 10 分钟)
问题:什么原因?如何改善呢?我可以使用任何经过充分验证的技术将大文本主干分成小的有效负载以避免“浏览器挂起”问题吗?
背景:我一直在学习使用 webapp2 + Google App Engine 进行 python 网络编程。我正在尝试使用 jQuery Ajax 构建一个“所输入即所见”的编辑区。它与提供实时预览功能的 stackoverflow post editor: wmd-input vs wmd-preview 非常相似。 (它一直提示'草稿已保存'到短文本。另一个例子是Google Docs实时编辑功能)
我的例子是这样的:textchange jQuery 插件触发由每个输入文本区域更改触发的 Ajax 发布 ---> Python 后端接收文本并在其上添加一些消息 ---> 发回文本+messages ---> jQuery 使用服务器响应更新预览文本区域(嗯,发回接收到的文本的全部内容只是为了测试目的。)
我的前端代码:
<script type="text/javascript">
function simpleajax() {
$.ajax({
type: 'POST'
,url: '/simpleajax'
,dataType: 'json'
,data:{'esid':'#ajaxin','msgin':$('#ajaxin').val()}
,cache:false
,async:true
,success:function (resp){$('#ajaxout').text(resp.msgout);}
,error:function (jqXHR, textStatus, errorThrown){
{$('#ajaxout').text("Ajax Error:"+textStatus+","+errorThrown)}}
});
}
$(document).ready(function(){
$('#ajaxin').bind('textchange', function () {
$('#ajaxstatus').html('<strong color="blue">Typing ...</strong>');
simpleajax();
});
});
</script>
我的后端代码:
class simpleajax(BaseReqHandler):
def get(self):
content={'pagealert':'simpleAjax get response'}
self.render_to_response('simpleAjax.html',**content)
def post(self):
esid=self.POST['esid']
msgin=self.POST['msgin']
msgout="Server noticed element " + esid + " value changed" + " and saved following message: " + msgin
content={"msgout":msgout}
self.writeout(content)
测试用例和症状:本地服务器 + 纯文本负载
将小于 500KB 的纯文本复制并粘贴到输入区域:非常有效。然而,一个 1.7MB 的文本会使浏览器在 >10 分钟内忙碌,看起来完全没有响应。
比较:我将相同的文本粘贴到 stackoverflow 帖子编辑器,预览立即出现!这次我没有注意到草稿保存的提示。并且这里有一些判断文本长度的javascript代码。出色地。有可能不涉及服务器通信。 但这只是一种变通方法,而不是解决我的问题的方法。(Google 文档自动保存功能必须利用某种技术来解决这个问题!)
Firebug xhr 监控结果:
#Request Headers:
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
X-Requested-With: XMLHttpRequest
Content-Length: 2075974
Referer: http://localhost:8080/ajax
Cookie: __utma=111872281.1883490050.1319630129.1319630129.1319637523.2; __utmz=111872281.1319630129.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Pragma: no-cache
Cache-Control: no-cache
#Response Headers:
Server: Development/1.0
Date: Fri, 04 Nov 2011 03:29:05 GMT
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Content-Length: 1790407
#Firebug Timeline:
TimePoints TimeElapsed Actions
0 1ms DNS Lookup
+1ms 1ms Connecting
+2ms 82ms Sending
+84ms 1.03s Waiting
+1.11s 14m22s Receiving
+14m23.11s Done
有趣的事情:
这背后发生了什么?任何帮助表示赞赏!我想让服务器在 Ajax 请求后将很多东西“推送”到客户端,但这个问题使它变得不可能。
最佳答案
考虑结合使用 HTML5 WebSockets 和 Worker API 从服务器异步发送/接收数据,而不影响 UI 线程性能。
http://dev.w3.org/html5/websockets/
http://net.tutsplus.com/tutorials/javascript-ajax/start-using-html5-websockets-today/ (教程假设PHP为服务端技术)
http://www.whatwg.org/specs/web-apps/current-work/multipage/workers.html
另一种选择
a) 在 mousedown
和 keyup
上
- record the cursor position in the text-box - store it in C1.
b) 在 textchange
> record the cursor position - store it in C2.
> send only the content between C1 and C2 to your server. Also send the values C1 and C2 to the server, i.e your AJAX payload will look something like:
{ c1: C1, c2: C2, data: <text in the text-box from C1 to C2> }
您需要查看是否 c1 > c2
,并适本地获取子字符串,反之亦然
这样,每次只将“更改”发送到服务器 - 而不是全文。但是,如果您复制并粘贴 5MB 的内容,则不会有任何改善。但对于单个字符的更改(如键入、粘贴小段等),这应该工作得很好。
关于jquery - 如何在大数据发布中使用 jQuery Ajax 加速 HTTP 响应接收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8005042/
我想在我的 iPhone 应用程序中加入线性回归。经过一些搜索,我发现 Accelerate Framework 中的 LAPACK 和 BLAS 是正确的库。但是我很难将加速框架添加到我的 XCod
有什么方法可以加速 JS 脚本(我指的是一些复杂的 DOM 操作,比如游戏或动画)? 最佳答案 真的没有办法真正加快速度。您可以压缩它,但不会快很多。 关于Javascript 加速?,我们在Stac
有时,我必须为一个项目重新导入数据,从而将大约 360 万行读入 MySQL 表(目前是 InnoDB,但我实际上并不局限于这个引擎)。 “加载数据文件...”已被证明是最快的解决方案,但它有一个权衡
在尝试计算加速时,我被卡住了。所以给出的问题是: 问题 1 如果程序的 50% 增强了 2 倍,其余 50% 增强了 4 倍,那么由于增强而导致的整体加速是多少? Hints:考虑增强前(未增强)机器
目前我正在处理实时绘图,但可视化非常慢。我想知道你可以做些什么来加速 Matplotlib 中的事情: 后端如何影响性能?是否有后端 实时绘图比其他人更好吗? 我可以降低分辨率以提高 FPS 吗? 如
我有一个小型测试框架。它执行一个循环,执行以下操作: 生成一个小的 Haskell 源文件。 使用 runhaskell 执行此操作.该程序生成各种磁盘文件。 处理刚刚生成的磁盘文件。 这种情况发生了
这是我的网站:Instant-YouTube 如您所见,加载需要很长时间。在 IE8 及以下甚至有时会导致浏览器崩溃。我不确定是什么原因造成的。可能是 Clicksor 广告,但我认为是 swfobj
是否可以加速 SKSpriteNode? 我知道可以使用 node.physicsBody.velocity 轻松设置速度但是设置它的加速度有多难? 最佳答案 从牛顿第二定律倒推运动:F = m.a您
有没有人有加速 FCKEditor 的技术?是否有一些关键的 JavaScript 文件可以缩小或删除? 最佳答案 在最新版本 (3.0.1) 中,FCKEditor 已重命名为 CKEditor .
我有以下 MySQL 查询,需要一天多的时间才能执行: SELECT SN,NUMBER FROM a WHERE SN IN (SELECT LOWER_SN FROM b WHER
我现在正在开发一款使用加速来玩的游戏。我找到了如何让我的元素移动,但不改变它的“原点”,或者更准确地说,改变加速度计算的原点: 事实上,我的图像是移动的,它的中心是这样定义的: imageView.c
我有一个 mysql 表,其中存储有 4 列的成员消息: message_id(主键,自增) sender_id( key ) receiver_id( key ) 消息内容 我做了很多 SELECT
我在 cuda_computation.cu 中有以下代码 #include #include #include #include void checkCUDAError(const char
我正在使用 BeautifulSoup 在 for 循环中解析数千个网站。这是我的代码片段: def parse_decision(link): t1 = time.time() de
我正在使用 OpenCV 2.4 (C++) 在灰度图像上进行寻线。这涉及一些基本的图像处理步骤,如模糊、阈值、Canny 边缘检测器、梯度滤波器或霍夫变换。我必须在数千张图像上应用寻线算法。 考虑到
当我试图连续生成四次相同的报告时,我刚刚分析了我的报告应用程序。第一个用了 1859 毫秒,而后面的只用了 400 到 600 毫秒。对此的解释是什么?我能以某种方式使用它来使我的应用程序更快吗?报告
当我打开 Storyboard文件时,由于其中包含的 VC 数量,打开它需要 1-2 分钟。加快速度的最佳做法是什么?我们应该将一些 VC 移动到不同的 Storyboard文件中吗?我们是否应该使用
我有一个包含多个页面的 UIPageViewController。每个页面都是相同的 View Controller ,但会跟踪页码并显示 PDF 的正确页面。问题是每个 PDF 页面都需要在 cur
这实际上是两个问题,但它们非常相似,为了简单起见,我想将它们放在一起: 首先:给定一个已建立的 Java 项目,除了简单的代码内优化之外,还有哪些不错的方法可以加快它的速度? 其次:在用Java从头写
我有一个包含 1000 个条目的文档,其格式类似于:
我是一名优秀的程序员,十分优秀!