本文介绍了PHP MySQLi Singleton for Ajax-Requests终止于许多进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用AJAX来获取信息的PHP应用程序-在后面,它使用了PHP MySQLi单例. AJAX请求每0.5秒发送一次,它们从数据库中读取一些内容并将其作为JSON字符串传递回网站.当我多次打开网站(在不同的选项卡中)时,我得到一个错误,因为PHP或apache无法分叉另一个进程".我的服务器有足够的RAM,但是问题是达到了130个进程的进程限制.

I have a PHP application which uses AJAX to get information - in the back, it uses a PHP MySQLi singleton. The AJAX-requests are send every 0.5 second and they read some stuff out of the database and deliver it as a JSON String back to website.When I open the website several times (in different tabs), I then get an error because PHP, or rather apache, could not "fork another process". My server has enough RAM, but the problem is taht the process-limit of 130 processes was reached.

cat /proc/user_beancounters
---------------------------
        | held  | maxheld |
numproc |  130  |     130 |

所以,我想知道单例是否有故障(例如为什么单例不好"),或者您能想象另一个错误源吗?错误源,许多MySQL进程开始启动并达到最大值.限制?

So, I would like to know if it's possible that the singleton is fault (like "why singletons are bad") or could you imagine another error source? The error source, that so many MySQL-processes get started and reach the max. limit?

推荐答案

我建议您发布单例和ajax后端的代码.如果我们无法查看的话,很难说您的Singleton或Ajax后端有问题.

I would recommend you to post the code of your singleton and ajax back-end. It's hard to make a statement saying that your singleton or ajax back-end is at fault if we are not able to look at it.

我可能会把连接数看作是一个更可能的原因.如果这不是本地应用程序,则实际请求所花费的时间超过0.5s并不少见.因此,在调用另一个请求之前,可能不会完成一个请求.随着时间的流逝,这可能会建立一个队列,使SQLi后端无法足够快地处理,并且由于每个连接的客户端每0.5秒将新请求添加到队列中,因此处理计数将开始增加.

I would probably look at the number of connections as a more likely cause though. If this is not a local application it's not uncommon that the actual requests take more than 0.5s. So one request might not be done before you call another. This could over time build up a queue that the SQLi backend is not able to process fast enough and the process count would start to rise since new requests were added to the queue every 0.5s for each connected client.

ajax后端的外观如何?它会立即运行查询吗?它会启动另一个php实例吗? Web服务器和数据库配置了多少个并发连接?所有这些都将在很大程度上决定您的应用程序的行为方式.另外,如上所述.请注意,即使您使用单例,它也不会执行您的Ajax调用而.每次您打电话时,您的php应用程序都会从​​头开始,并且您的单例会再次从头开始创建.

How does the ajax back-end look? Does it run a query straight away? Does it launch another php instance? How many concurrent connection is the webserver and database configured for?All these things will heavily determine how your application behaves. Also, as stated. Note that even if you use a singleton it does NOT live past the execution of your ajax call. Each time you make a call your php application starts from scratch and your singleton is created from scratch once again.

您可以设置一个应用程序(用PHP或其他语言编写),该应用程序实际上将一直运行并利用主循环.然后,您可以使它侦听特定的套接字,或者通过其他方式使ajax后端将数据传递给该应用程序.然后,运行中的应用程序可以处理数据库后端,并将数据返回到ajax后端,该ajax后端将返回答复.这样,数据库处理程序将不会一直重新创建您的单例.但是,如果有很多客户端发出要求每0.5秒查询一次数据库的请求,您仍然很可能会遇到队列问题.

You could set up an application (written in PHP or other) that would actually run all the time and make use of a main loop. Then you could either make that listen to a specific socket or by other means have your ajax back-end pass data to that application. The running application could then handle the database back-end and return the data to the ajax back-end that would return the reply. This way the database handler would not recreate your singleton all the time. But you would very likely still run into queue issues if there are a lot of clients making requests that require a database look-up every 0.5s.

祝你好运!

这篇关于PHP MySQLi Singleton for Ajax-Requests终止于许多进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:46