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.
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.
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.
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.
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!
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.
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.
Firstly, seriously think about the following questions:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With