如何最好地处理需要很长时间才能完成的Ajax请求? [英] How do I best handle ajax requests that take a long time to complete?

查看:62
本文介绍了如何最好地处理需要很长时间才能完成的Ajax请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我在开始这个问题时先说一下,我在设计处理器密集型Web应用程序方面相对较新.我有一个基于LAMP堆栈的功能性应用程序,现在正处于开发阶段,我必须实现规模化.

Let me preface this question by saying I'm relatively new to architecting processor intensive web applications. I've got a functional application built on a LAMP stack, and am now at the point in development where I have to implement for scale.

我有一个运行PHP代码的Web服务器,该服务器通过SSH进入远程Windows计算机以执行批处理,根据并发性,该批处理可能需要15到45秒才能完成.我正在使用 phpseclib pear库软件包提供的SSH2实现登录到远程计算机以启动批处理文件与 Sysinternals的PsExec .PHP代码如下所示:

I've got a web server running PHP code that ssh's into a remote Windows machine to execute a batch process that can take about 15 to 45 seconds to complete depending on concurrency. I'm using the SSH2 implementation provided by the phpseclib pear library package to login to the remote machine to launch the batch file with Sysinternals' PsExec. The PHP code looks something like this:

$remoteCommand = 'psexec -u username -p password -h cmd /C "C:\\automate_process.bat >> automate_process.log 2>>&1"';
$ssh_connection->exec($remoteCommand);

这实际上是从托管php代码的Web服务器在远程Windows计算机上调用automate_process.bat.但是,automate_process.bat文件需要一段时间才能执行,并且如果有并发用户,有时会导致Web服务器的PHP超时.增加php.ini中的max_execution_time值不能解决问题.

This essentially invokes automate_process.bat on the remote Windows machine from the web server hosting the php code. However, the automate_process.bat file takes a while to execute and will sometimes cause the web server's PHP to timeout if there are concurrent users. Increasing the max_execution_time value in php.ini doesn't solve the issue.

使用PsExec, -d 开关将不等待automate_process.bat完成,并且将允许PHP脚本继续运行而无需等待.因此,在max_execution_time前面解决了问题,但是又出现了另一个问题:通知最终用户的浏览器automatic_process已完成.如何有效地做到这一点?似乎需要对应用程序代码进行体系结构更改.

With PsExec, the -d switch will not wait for automate_process.bat to complete, and will allow the PHP script to continue without waiting. So, problem solved on the max_execution_time front, but another issue arises: notifying the end-user's browser that the automated_process is complete. How can this be done in an effective manner? What is seemingly needed is an architectural change in the application code.

起初,我认为可以通过某种轮询来解决问题-也许是长时间轮询?我只是要对远程Windows机器执行ping操作,以查看该过程是否完成.但是我读到的所有内容都表明,长轮询是Apache的沉重负担,而远程Windows机器正在运行Apache.此外,网络上的大量文章都将Node.js作为解决方案.这真的是解决此类问题的方法吗?我是否必须学习一个新的框架以使该操作在应用程序扩展时变得强大起来?我对此表示满意,但我想知道是否可以将n和PHP和Node.js混合使用.如果是这样,有人可以提供一个简单的示例来帮助我入门吗?

At first I thought some kind of polling would do the trick--maybe long-polling? I would just ping the remote windows Machine to see if the process was complete. But everything I read says that long-polling is a severe burden on Apache which is what the remote Windows machine is running. In addition, a good deal of writing on the web points to Node.js as a solution. Is that really the solution to this type of problem? Do I have to learn a new framework to make this operation robust as the application scales up? I'm fine with that, but I want to know if I can mix-n-match PHP and Node.js. If so, can anybody provide a simple example to get me started?

谢谢!

推荐答案

如果处理时间已到,我将设置客户端以使用初始ajax请求启动处理.服务器将启动该过程并回复处理中".从客户端发出的ajax请求之间的间隔可以重复10秒,直到过程完成为止,您只需要跟踪服务器上的每个客户端即可.

If the processing time is what it is, I would setup the client to initiate the processing with the initial ajax request. The server would start the process and reply 'processing'. The cycle could be repeated with a 10 second delay between ajax requests from the client until the process is finished, you just have to track each client on the server...

这篇关于如何最好地处理需要很长时间才能完成的Ajax请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆