您如何在PHP/MySQL应用程序中充分利用多核CPU? [英] How do you make good use of multicore CPUs in your PHP/MySQL applications?

查看:394
本文介绍了您如何在PHP/MySQL应用程序中充分利用多核CPU?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我维护着一个定制的类似CMS的应用程序.

I maintain a custom built CMS-like application.

无论何时提交文档,都会执行一些任务,这些任务可以大致分为以下几类:

Whenever a document is submitted, several tasks are performed that can be roughly grouped into the following categories:

  1. MySQL查询.
  2. HTML内容解析.
  3. 搜索索引更新.

类别1包括对与文档内容有关的各种MySQL表的更新.

Category 1 includes updates to various MySQL tables relating to a document's content.

类别2包括解析存储在MySQL LONGTEXT字段中的HTML内容,以执行一些自动锚标记转换.我怀疑在此任务上花费了大量的计算时间.

Category 2 includes parsing of HTML content stored in MySQL LONGTEXT fields to perform some automatic anchor tag transformations. I suspect that a great deal of computation time is spent in this task.

类别3仅使用与文档相对应的几个字段就对基于MySQL的简单搜索索引进行了更新.

Category 3 includes updates to a simple MySQL-based search index using just a handful of fields corresponding to the document.

所有这些任务都需要完成,才能将文档提交视为完成.

All of these tasks need to complete for the document submission to be considered complete.

承载此应用程序的计算机具有双四核Xeon处理器(总共8个内核).但是,无论何时提交文档,所有执行的PHP代码都被限制在一个内核上运行的单个进程中.

The machine that hosts this application has dual quad-core Xeon processors (a total of 8 cores). However, whenever a document submits, all PHP code that executes is constrained to a single process running on one of the cores.

我的问题:

您使用什么方案(如果有)在多个CPU内核之间分配PHP/MySQL Web应用程序处理负载?我理想的解决方案基本上是产生几个进程,让它们在几个内核上并行执行,然后阻塞直到所有进程都完成.

What schemes, if any, have you used to split up your PHP/MySQL web application processing load among multiple CPU cores? My ideal solution would basically spawn a few processes, let them execute in parallel on several cores, and then block until all of the processes are done.

相关问题:

您最喜欢的PHP性能分析工具是什么?

What is your favorite PHP performance profiling tool?

推荐答案

PHP并非完全面向多线程:如您已经注意到的,每个页面由一个PHP进程提供服务-一次只做一件事,包括在数据库服务器上执行SQL查询时仅等待".

PHP is not quite oriented towards multi-threading : as you already noticed, each page is served by one PHP process -- that does one thing at a time, including just "waiting" while an SQL query is executed on the database server.

不幸的是,您对此无能为力:这就是PHP的工作方式.

There is not much you can do about that, unfortunately : it's the way PHP works.


不过,这里有一些想法:


Still, here's a couple of thoughts :

  • 首先,您可能一次在服务器上拥有1个以上的用户,这意味着您将同时提供多个页面,这又意味着您将拥有多个PHP进程, SQL查询同时运行...这意味着将使用服务器的多个内核.
    • 每个PHP进程将在一个内核上运行,以响应一个用户的请求,但是Apache有几个并行运行的子进程(每个请求一个子进程,最多数十个或数百个) ,具体取决于您的配置)
    • MySQL服务器是多线程的,这意味着它可以使用几个不同的核心来回答多个并发请求-即使每个请求不能由一个以上的核心服务.
    • First of all, you'll probably have more that 1 user at a time on your server, which means you'll serve several pages at the same time, which, in turn, means you'll have several PHP processes and SQL queries running at the same time... which means several cores of your server will be used.
      • Each PHP process will run on one core, in response to the request of one user, but there are several sub-processes of Apache running in parallel (one for each request, up to a couple of dozens or hundreds, depending on your configuration)
      • The MySQL server is multi-threaded, which means it can use several distinct cores to answer several concurrent requests -- even if each request cannot be served by more that one core.

      因此,实际上,您服务器的8核最终将被使用;-)

      So, in fact, your server's 8 core will end up being used ;-)


      而且,如果您认为自己的页面生成时间太长,则可能的解决方案是将计算结果分成两组:


      And, if you think your pages are taking too long to generate, a possible solution is to separate your calculations in two groups :

      • 一方面,生成页面所需要做的事情:对于这些人来说,您无能为力
      • 另一方面,有时必须运行的事物,但不一定立即运行
        • 例如,我正在考虑一些统计数据计算:您希望它们是最新的,但是如果它们落后几分钟,那通常就可以了.
        • 与发送电子邮件相同:无论如何,您的用户在接收/阅读邮件之前会经过几分钟,因此不需要立即发送邮件.
        • On one hand, the things that have to be done to generate the page : for those, there is not much you can do
        • On the other hand, the things that have to be run sometimes, but not necessarily immediately
          • For instance, I am think about some statistics calculations : you want them to be quite up to date, but if they lag a couple of minutes behind, that's generally quite OK.
          • Same for e-mail sending : anyway, several minutes will pass before your users receive/read their mail, so there is no need to send them immediately.

          对于我第二点的情况,因为您不需要立即完成这些工作...好吧,就是不要立即进行;-)
          我经常使用的解决方案是某种排队机制:

          For the kind of situations in my second point, as you don't need those things done immediately... Well, just don't do them immediately ;-)
          A solution that I often use is some queuing mechanism :

          • Web应用程序将内容存储在待办事项列表"中
          • "todo-list"被一些经常通过cronjob运行的批处理出队

          对于其他一些操作,您只希望它们每隔X分钟运行一次-而且,在这里cronjob也是理想的工具.

          And for some other manipulations, you just want them run every X minutes -- and, here too, a cronjob is the perfect tool.

          这篇关于您如何在PHP/MySQL应用程序中充分利用多核CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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