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条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式