PHP多线程编程 之 基本概念(官方文档)

Share Nothing, Do Everything 🙂

PHP has an awesome amount of extensions and features, but was conceived at a time when Web Servers were far less powerful than they are today. Almost every web server ( and mobile phone ! ) has multiple cores, if not multiple CPU’s with multiple cores. PHP does not take advantage of this very well at all. While PHP is very fast to develop and express your ideas, when developing enterprising web applications we often find ourselves looking to other fully fledged languages and frameworks to write backend, or data-centric web applications because PHP isn’t able to take advantage of the hardware we have at our disposal. Being able to thread in PHP makes it a real candidate for enterprising applications, and it’ll make your personal home page be able to do things it simply didn’t have the time to do before.
PHP具有大量的扩展和功能,但是在构思它的时候Web服务器远没有今天那么强大。如今几乎每个Web服务器(和手机)都拥有多核心,如果不是多CPU带多核心。PHP根本没有利用到多核的优点。虽然PHP发展很快并激发你的灵感,当开发企业Web应用程序时,我们往往会寻找其他完全成熟的语言和框架来写后端或以数据为中心的Web应用程序,因为据我们所知PHP不能够充分利用硬件。能够支持线程能使PHP成为企业应用程序的候选者,并且它会让你的个人主页上可以做之前根本没有时间做的事情。

The Basics
pthreads is an Object Orientated API that allows user-land multi-threading in PHP. It includes all the tools you need to create multi-threaded applications targeted at the Web or the Console. PHP applications can create, read, write and synchronize with Threads, Workers and Stackables.
pthreads是一个面向对象的允许在PHP执行用户态多线程的API。它包含了所有你需要创建目标是Web或终端的多线程应用程序的工具。使用Threads,Workers和Stackable,PHP应用程序可以创建、读写和同步。
A Thread Object
The user can implement a thread by extending the Thread declaration provided by pthreads. Any members can be written and read by any context with a reference to the Thread, any context can also execute any public and protected methods. The run method of the implementation is executed in a separate thread when the start method of the implementation is called from the context ( that’s Thread or Process ) that created it. Only the context that creates a thread can start and join with it.
通过扩展pthreads提供的Thread定义,用户可以实现一个线程。带有对Thread引用的任意的上下文可以读写它的任何成员,任何的上下文也可以执行任何的它的公共的和受保护的方法。当实现类的start方法从创建它的上下文(那是线程或进程)被调用时,实现类的run方法在独立的线程中被执行。只有创建线程的上下文可以开始和join这个线程(初次接触肯定不知道join是何物,实际join就是等待线程返回)。
A Worker Object
A Worker Thread has a persistent state, and will be available from the call to start until the object goes out of scope, or is explicitly shutdown. Any context with a reference can pass objects of type Stackable to the Worker which will be executed by the Worker in a separate Thread. The run method of a Worker is executed before any objects on the stack, such that it can initialize resources that the Stackables to come may need.
一个Worker线程有一个持久化状态,并且从调用它的start方法后直到这个对象退出作用域都可用,除非它被明确关闭。任何的带有Worker对象的引用的上下文都可以传递Stackable类型的对象到Worker,这个Stackable类型的对象将被Worker放在一个独立的线程中执行。Worker的run方法在栈中的任何对象之前被执行,这样它可以初始化Stackable对象可能用到的资源。
A Stackable Object
A Stackable Object can read/write and execute the Worker Thread during the execution of it’s run method, additionally, any context with a reference to the Stackable can read, write and execute it’s methods before during and after execution.
Stackable对象的run方法执行时,它可以读写和执行Worker Thread(如何做到?Stackable对象压栈时把自身就是Worker传递给Stackable对象的$this->worker属性,这里没有说明),任何引用Stackable的上下文可以在执行之前、之间和之后读写和执行它的方法(这句话没有理解….)。
Synchronization
All of the objects that pthreads creates have built in synchronization in the form of ::wait and ::notify. Calling ::wait on an object will cause the context to wait for another context to call ::notify on the same object. This allows for powerful synchronization between Threaded Objects in PHP.
所有的pthreads创建的对象具有内置的以wait和notify形式的同步机制。在一个对象上调用wait将暂停上下去等待另一个上下文在相同的对象上调用notify(注意,是在另一个上下文调用,而且是同一个对象)。这允许在PHP中在Threaded对象之间进行功能强大的同步(一个场景是,创建两个线程对象,向需要同步的另一个对象传递引用)。
Wait, Threaded Objects ?
A Stackable, Thread or Worker can be thought of, and should be used as a Threaded stdClass: A Thread, Worker and Stackable all behave in the same way in any context with a reference.
Stackable,Thread或Worker可以认为和应该被用做一个Threaded StdClass(就是TMD Threaded是它们的父类):……..
Any objects that are intended for use in the multi-threaded parts of your application should extend the Stackable, Thread or Worker declaration, which means they must implement run but may not ever be executed; it will often be the case that Objects being used in a multi-threaded environment are intended for execution. Doing so means any context ( that’s Thread/Worker/Stackable/Process ) with a reference can read, write and execute the members of the Threaded Object before, during, and after execution.
任何的你打算在多线程中使用的对象都应该继承Stackable,Thead或Worker的声明,那意味着这些对象必须实现run方法,但是可能永不会被执行;它通常被认为在多线程环境中用于执行的情况(run方法在一个独立线程中运行)….
Method Modifiers
The protected methods of Threaded Objects are protected by pthreads, such that only one context may call that method at a time. The private methods of Threaded Objects can only be called from within the Threaded Object during execution.

Data Storage
As a rule of thumb, any data type that can be serialized can be used as a member of a Threaded object, it can be read and written from any context with a reference to the Threaded Object. Not every type of data is stored serially, basic types are stored in their true form. Complex types, Arrays, and Objects that are not Threaded are stored serially; they can be read and written to the Threaded Object from any context with a reference.
根据经验,任何可以被序列化的数据类型都可以用来作为Threaded对象的成员,它可被任何有引用到Threaded对象的上下文读写(只要有引用就可以读写,注意是任何上下文)。不是所有的数据类都是序列化存储的,基础类型使用它原来的形式存储。那些不是Threaded对象的复杂类型,数组和对象也是序列化存储的;可以在任何线程中把它们读写到Threaded对象中,只要上下文有Threaded对象的引用。
With the exception of Threaded Objects any reference used to set a member of a Threaded Object is separated from the reference in the Threaded Object; the same data can be read directly from the Threaded Object at any time by any context with a reference to the Threaded Object.
在Threaded对象中除了Threaded对象,任何用来设置Threaded对象的成员的引用都是从引用分离的(意思是用来设置Threaded对象属性的引用,不管是值还是什么,除了Threaded对象,都是用它的一份拷贝,理解这个是很重要的,比如你希望传递一个Threaded对象外的引用(非Threaded引用,比如数组)初始化Threaded对象,你务必知道这是个克隆操作)
Resources
The extensions and functionality that define resources in PHP are completely unprepared for this kind of environment; pthreads makes provisions for resources to be shared among contexts, however most types of resource WILL have problems. Most of the time, resources should NOT be shared among contexts, and when they are they should be basic types like streams and sockets.
PHP中扩展或功能定义的资源完全没有为这种类型为环境而准备;对于在上下文之间被共享的资源pthreads做了规定,然而大多数的资源类型将有问题。大多时候,资源不应该在上下文之间被共享,否则它们应该是基本的类型,比如streams和sockets。
Officially, resources remain unsupported.

A Work in Progress

pthreads was and is an experiment with pretty good results. Any of its limitations or features may change at any time; that is the nature of experimentation. It’s limitations – often imposed by the implementation – exist for good reason. The aim of pthreads is to provide a useable solution to multi-tasking in PHP at any level, in the environment which pthreads executes, some restrictions and limitations are necessary in order to provide a stable environment.

永久链接:http://blog.ifeeline.com/1127.html