简体   繁体   English

如何解决在PHP后端未同时执行的Ajax调用的问题?

[英]How to troubleshoot ajax calls not being executed simultaneously in PHP backend?

I am developing a web app that needs to call functions from a web service using SOAP. 我正在开发一个Web应用程序,该应用程序需要使用SOAP从Web服务中调用函数。

I have coded a simple PHP backend to access the SOAP functions. 我已经编写了一个简单的PHP后端以访问SOAP函数。 The backend mainly consists of classes with static "helper" methods (I use __autoload() to load the classes only when needed). 后端主要由带有静态“ helper”方法的类组成(我仅在需要时使用__autoload()加载类)。

At some point in the app, I need to call two SOAP functions at the same time. 在应用程序的某个时刻,我需要同时调用两个SOAP函数。 Since I want to speed up the process, I use two simultaneous ajax calls. 由于我想加快处理过程,因此我使用了两个同时的ajax调用。

The thing is, those calls don't always run simultaneously. 问题是,这些调用并非总是同时运行。 The requests are sent at the same time, but the second call lasts longer than the first one (they should have roughly the same run time). 请求是在同一时间发送的,但是第二个呼叫的持续时间比第一个呼叫的持续时间长(它们应该具有大致相同的运行时间)。 If the first call lasts 600 ms, the second one lasts 1400-1600 ms. 如果第一个通话持续600毫秒,第二个通话持续1400-1600毫秒。

I have setup a simple test, where I call both functions first in parallel and then, when both requests are complete, in serial one after the other. 我已经设置了一个简单的测试,在该测试中,我首先并行调用两个函数,然后在两个请求完成后依次调用另一个函数。

请求测试,通话时间更长

I have tried profiling the PHP side (both using microtime() and xdebug), but I've consistently found that the calls have the same execution time on PHP (about 500ms). 我曾尝试对PHP端进行性能分析(都使用microtime()和xdebug),但是我一直发现,这些调用在PHP上具有相同的执行时间(大约500毫秒)。

What is REALLY driving me insane, though, is the fact that the very same code seems to work and break in an apparently random fashion. 但是,真正使我发疯的是事实,就是相同的代码似乎以随机的方式工作和破坏。 Sometimes I reload the page and the calls are executed simultaneously. 有时我会重新加载页面,并且调用会同时执行。 Then I reload again, and the second call takes longer, once more. 然后,我再次重新加载,第二次通话又花了更长的时间。

请求测试并获得预期结果

PHP Session lock should not be an issue, I open the session only when needed and close it with session_write_close() afterwards. PHP会话锁定应该不是问题,我仅在需要时打开会话,然后再使用session_write_close()将其关闭。

The SOAP server called by the php backend supports multiple connections, and besides my microtime() profiling has shown me that it takes about 400ms to complete a SOAP request, so I don't think the web service is to blame. 由php后端调用的SOAP服务器支持多个连接,并且除了我的microtime()分析外,我还需要大约400毫秒才能完成SOAP请求,因此我不认为应该归咎于Web服务。

On the client side, the requests are sent at the same time. 在客户端,请求是同时发送的。 I tried adding a slight delay to the second call, to no avail. 我尝试将第二次通话稍加延迟,但无济于事。

But then again, this "random" behaviour is baffling me. 但是话又说回来,这种“随机”行为令我感到困惑。 I have tested the app both on local (WAMP) and on two different servers (shared hosting). 我已经在本地(WAMP)和两个不同的服务器(共享主机)上测试了该应用程序。 In all of the environments, PHP 5.5 was used. 在所有环境中,都使用PHP 5.5。

What could be causing this discrepancy, and how could one try to isolate the problem, if the behaviour is not 100% consistent? 是什么导致这种差异,并且如果行为不是100%一致的,又该如何解决该问题?

I've solved the mystery. 我解决了这个谜。

In my local environment, once I disabled xdebug, the ajax calls ran flawlessly in parallel. 在我的本地环境中,禁用xdebug后,ajax调用可以完美并行地运行。

In remote, I still had the same problem (and no xdebug at all). 在远程环境中,我仍然遇到相同的问题(根本没有xdebug)。 So first of all, I've tried using one single AJAX call to a PHP script that used curl_multi_exec() to run the SOAP requests in parallel, basically a similar setup but mainly server-side. 因此,首先,我尝试对PHP脚本使用单个AJAX调用,该脚本使用curl_multi_exec()并行运行SOAP请求,基本上是类似的设置,但主要是服务器端的。

I found that I had the same discrepancies in the timings using this technique as well, with a very similar pattern. 我发现使用这种技术在时间上也有相同的差异,并且模式非常相似。

I've contacted the hosting provider explaining my problem, and they told me that it might be due to the shared environment having limited resources depending on the overall load. 我已经联系了托管服务提供商并解释了我的问题,他们告诉我,这可能是由于共享环境的资源有限,具体取决于整体负载。

So in the end while it's possible to achieve this behaviour, in my specific case I had to change my strategy and use two serial AJAX calls, updating the view after the first call to give the impression of a faster response while waiting for the second call to complete. 因此,最后虽然可以实现此行为,但在我的特定情况下,我不得不更改策略并使用两个串行AJAX调用,在第一个调用之后更新视图以给人以更快的响应印象,同时等待第二个调用去完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM