donglu953744 2011-07-01 08:39
浏览 44
已采纳

用于Ajax-Request的PHP MySQLi Singleton结束了许多进程

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 |

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?

  • 写回答

4条回答 默认 最新

  • douang4294 2011-07-06 07:02
    关注

    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.

    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.

    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.

    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.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存