Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is not a good idea to use multithreading in php?

I know php doesn't have threading. But in this tutorial they show that by using host operating systems ability to form we can achieve it. It has also said it to not do this in the production code. Why is it not a good idea?

Here is a sample code

$processID = pcntl_fork();
if($processID) {
     echo "I'm in the parent process!";
} else {
     echo "I'm in the child process!";
}

Here is the tutorial.

like image 495
odbhut.shei.chhele Avatar asked Dec 06 '22 00:12

odbhut.shei.chhele


1 Answers

Does forking create a Thread ?

When we fork a process, the process space, that is to say the region of memory where the libraries and code the process requires to execute reside, is duplicated, the distinct but related processes then continue to execute at the will of the operating systems scheduler in different regions of memory.

What is the difference between a Forked Process and a Thread ?

When we create a Thread we are telling the operating system that we want another unit of execution that can operate in the same region of memory as the Process that created it.

How different operating systems actually implement threads and processes is beyond the scope of this answer, and is unimportant.

Why is Forking a bad idea at the frontend ?

When you copy the whole address space, you duplicate the region of memory that the webserver is operating in too, this can obviously cause havoc for your operating system.

Why is Threading a bad idea at the frontend ?

If a client script instructs the operating system to create 8 threads in direct response to a web request, and 100 clients simultaneously request the script, you will be instructing your operating system to execute 800 threads concurrently.

CPUs and operating systems would need to look very very different to make that a good idea!

Where is Threading a good idea?

Multi-threaded software, and extremely capable hardware, is ubiquitous; computing would not be what it is without it.

In the context of Web infrastructure, mysql and other database servers are multi-threaded, indeed Apache can deploy PHP in a multi-threaded infrastructure, though I wouldn't recommend it.

When we look at how enterprising applications like mysql actually provide their extremely complex services, we can see that their process (and therefore threads) are completely isolated from the infrastructure of your web application.

This is how we use Threads in languages that support them; we design systems whose means of providing their services is via some sane form of IPC, we isolate our complex infrastructure, completely, from that which should be simple: our web applications.

Is PHP really suitable for Threads ?

The memory model for PHP is shared nothing: this means that each interpreter context, in the sense of the structures and memory PHP requires to operate, is isolated from any other context.

This always has to be true for PHP to work as intended; an implementation of threading for PHP that was ignorant of the way PHP worked simply would not function.

pthreads goes to great lengths to ensure the memory model is not broken, every Thread does indeed not share memory directly with any other Thread.

Are Threads really suitable for me ?

Firstly, seriously think about the following questions:

  • Is Threading really required ?
  • What other ways can you find to achieve whatever it is you are setting out to do ?

Multi-threaded software is complex by nature; something being complicated is no kind of excuse for avoiding it, in my opinion.

But be aware that multi-threaded software is fundamentally different to your average PHP application, you have to think about things you have never had to think about before, be aware of things that didn't matter before you started your first Thread.

You should not guess at what these things are, you should seek to educate yourself in the subject as thoroughly as possible, and even be prepared to fail, and persevere.

The complexity of anything decreases as your knowledge increases, that's how learning works, here is where it begins:

https://gist.github.com/krakjoe/6437782

It continues in the manual, in the many examples distributed with pthreads, in stackoverflow searches and questions, and results in glory, in my opinion.

like image 154
Joe Watkins Avatar answered Dec 16 '22 08:12

Joe Watkins