PHP中的多线程/多任务 [英] Multi Threading / Multi Tasking in PHP

查看:237
本文介绍了PHP中的多线程/多任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我们通常在不考虑服务器功能的情况下进行编码.现在,甚至PC都具有多个内核,并且还可以处理64位数据.据我所知,PHP引擎本身已经过优化,可以利用多个内核.我们的程序员如何才能进一步优化代码以利用多个内核.

In PHP we normally do coding without considering what the server is capable of. Now a days even PCs have multiple cores and process 64 bit data also. As far as I know the PHP engine itself is optimised to take advantage of multiple cores. How can we programmers can optimize further the code to take advantage of multiple cores.

换句话说,我想知道可以教我编写代码的技术,而php引擎更可能考虑使用这些代码来处理并行性.

In other words I want to know the techniques that will teach me to write code which will be more likely to be considered by php engine to process parallely.

我不是要使用任何用户定义的/开放源代码的排队方法,而是以利用多核并更快地工作的方式编写相同的代码.

I'm not asking for any user defined / open source queuing method but to write the same code in such a way that it takes advantage of multi core and works faster.

如果您已经在做这样的事情,请提出您的想法并分享您的经验.

Please suggest your thoughts and share your experience if you already are doing something like this.

我希望应该有办法进一步优化代码.

I am hoping there should be way we can optimize code further.

推荐答案

自2000年5月22日PHP4首次发布以来,PHP就有很长时间的线程模型.

PHP has had a threading model for a very long time, since the first release of PHP4, May 22nd 2000.

在Web应用程序的前端创建用户线程没有任何意义.扩展非常困难. Apache Worker MPM二进制文件和mod_php所采用的每个客户端模型的线程并不是您真正想要用于服务网站的东西,当然,如果您正在使用它,则不想创建直接响应任何Web请求的其他线程.

Creating user threads at the frontend of a web application doesn't make any sense; it is extremely difficult to scale. The thread per client model that the Apache Worker MPM binary and mod_php employ is not really something you want to use to serve your websites, certainly if you are using it, you do not want to create additional threads in direct response to any web requests.

您可能经常听到开发人员说,在没有提供此类断言的基本原理的情况下,前端的线程是没有意义的.当您学习以必需的方式考虑系统时,问题变得很明显:

You may often hear developers say threads at the frontend do not make sense, without providing the rationale for such an assertion. When you learn to think about systems in the required way the problem becomes obvious:

如果客户端脚本直接响应Web请求创建了8个线程,而100个客户端同时请求该脚本,则说明您的硬件要求同时执行 800 个线程.

If a client script creates 8 threads in direct response to a web request, and 100 clients request the script simultaneously, you are requesting that your hardware execute 800 threads concurrently.

CPU的外观和工作确实必须有很大的不同才能使它成为一个好主意

进取的解决方案很可能有一个面向公众的PHP网站,但是系统的实际大脑是用语言编写的,这些语言对构建进取的解决方案所需的内容提供了良好的支持,例如Java,C#,C ++或任何其他语言.每天都是.

Enterprising solutions might well have a PHP website facing the public, but the actual brains of the system are written in languages that have good support for those things you require to build enterprising solutions such as Java, C#, C++ or whatever the language-of-the-day is.

您应该以相同的方式使用pthreads.通过设计组成部分彼此分开的系统,仅通过设计良好的高性能(RPC)API进行连接,这样,设计多线程体系结构所固有的复杂性便完全与面向公众的网站隔离了,并且简单,这样的网站将需要的可扩展设置.

You should use pthreads in the same way; by designing systems whose component parts are separated from one another, only connected by well designed, high performance (RPC) API's, such that the complexity inherent in designing a multi-threaded architecture is isolated completely from your public facing websites, and the simple, scalable setup that such a website will require.

让我们从Hello World的开头开始:

Let's start at the beginning with Hello World:

<?php
class My extends Thread {
    public function run() {
        printf("Hello World\n");
    }
}

/* create a new Thread */
$my = new My();

/* start the Thread */
$my->start();

/* do not allow PHP to manage the shutdown of your Threads */
/* if a variable goes out of scope in PHP it is destroyed */
/* joining explicitly ensures integrity of the data contained in an objects */
/* members while other contexts may be accessing them */
$my->join();
?>

无聊,但我希望你能读懂;)

Boring, but I hope you read it ;)

因此,在真实的系统中,您实际上并不想如此显式地创建线程,而您肯定希望只是将任务提交给某些复杂系统中的所有复杂系统的执行者服务,我见过使用这样的东西...

So in a real system, you don't really want to be creating threads so explicitly, you surely want to just submit tasks to some executor service, all of the complex systems, in the sense of their multi-tasking requirements, I have ever seen use such things ...

<?php
class My extends Threaded {
    public function run() {
        printf("Hello World from %s#%lu\n",
            __CLASS__, Thread::getCurrentThreadId());   
    }
}

/* create a Pool of four threads */
/* threads in a pool are created when required */
$pool = new Pool(4);

/* submit a few tasks to the pool */
$tasks = 100;
while ($tasks--) {
    $pool->submit(new My());
}

/* shutting down the pool is tantamount to joining all workers */
/* remember what I said about joining ? */
$pool->shutdown();
?>

我已经为您简要介绍了复杂的事情,您应该努力阅读所有可能的内容:

I have given you very brief explanations of complicated things, you should endeavor to read all you can:

  • https://gist.github.com/krakjoe/6437782
  • https://gist.github.com/krakjoe/9384409
  • http://php.net/pthreads

可以在此处找到许多示例: https://github.com/krakjoe/pthreads /tree/master/examples

Many examples can be found here: https://github.com/krakjoe/pthreads/tree/master/examples

免责声明:使用线程的服务器体系结构并没有什么真正的问题,但是当您开始创建其他线程时,您就限制了它的可伸缩性和按设计执行的能力,我可以想象到设计良好的体系结构确实具有在前端穿线的能力,但这并不是一件容易的事情.此外,对于高性能的针对Web的应用程序,线程并不是工具箱中唯一的东西.研究您所有的选择.

这篇关于PHP中的多线程/多任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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