PHP Threading

Published On: 29 July 2016.By .
  • Digital Engineering

A thread is a small unit of instructions which can be executed by a processor. A application uses threading if it requires parallelism. In other words by a single program ,we can process multiple unit of instructions parallaly.

PHP request serving capabilities are not multithreaded, for every request a new php process is invoked and served. If your server is not multi-core then parallelism can not take place.( It will be more timely based processor allocation)
PHP do not give inbuilt multi-threading functionality, we need to add package/extension “pthreads” to our php.


Pthread extension comes with lots of different concept, the basic three classes are listed here for kick start :

Threaded Objects : A class which is unit of executable instructions (thread), It is what you want to executed asynchronously. the run method in this class has ability to execute it as a thread.

Workers : It is a class which is used to handle threads, make results synchronized.

Pools : This class is used to handle multiple worker class instances and managing them.

A simple Example for thread

Pthread with autoload & context inheritance

All the examples are not related to the real world application where we need to put threading to our framework like YII 2, Slim etc. Where autoload functionality loads the library and the application but with child thread (It a single unit of processing of your own process) it may not relate to the parent context means there can be issue of Class not found even when the class is already loaded to the parent context.
In this case we can call autoload functionality to each thread but it will make things slower. We can solve the autoloading issue with the help of Worker class stacking or Pooling with the help of worker class.

Thread can customize inheritance of context, when we start the thread. Default is inherit all.

PTHREADS_INHERIT_CLASSES : Inherit only classed
PTHREADS_INHERIT_CONSTANTS : Inherit only constants
PTHREADS_INHERIT_FUNCTIONS : Inherit only functions
PTHREADS_INHERIT_NONE : Inherit nothing from parent context.

In case of worker with pooling if you want to set inherit property of a thread, we simple can not because the start method of the thread is called by the worker class. we need to override the start method here is an example :

Uses of threading :

We can use threading for notification engine, scheduler engine, or for any kind of batch processing for php application.

Pthread with Task Scheduling : A Real Life Example

My application required a scheduler engine to process tasks in bulk, where some other components are keep pushing tasks to queue. So the basic task of the scheduler engine is to process tasks at the time they meant / scheduled to be with some priority basis.

Before pthread implementation

First Try

We were using cron job and were keep creating cron jobs for tasks to execute. In this case we were using system’s scheduler to hit a process which double checks the job and make things execute.
This was a layman way to complete the job, we figured out that the number of tasks are too high and the server can go into a deadlock state.

Some Evolution

We got that the previous strategy is a failure for huge applications, now we created a domain process with diagnosis script, the process keeps seeking the job with some logic, if it finds any, it executes. On the other hand the diagnosis script is keep watching the domain process, if the process quits or terminates then the script’s job is to make the domain process run again.

And here comes Pthread in picture

The domain strategy was doing fine but later on the tasks were too many with priority logic and things were getting real slow, scheduled task getting late to run and again we were in a situation where we can not go ahead.
So we implemented pthread with our domain process strategy and pooling where domain process seeks jobs and when a job became mature, it executes it by creating a new thread.

Here is code of the sample scheduler engine with mongodb

Related content

Got an Idea?

Let’s create a better tomorrow together