Reputation: 6254
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.
Upvotes: 4
Views: 10990
Reputation: 17158
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.
Upvotes: 16
Reputation: 15969
PHP is supposed to run on webserver serving the frontend. In a typical environment you have multiple users (web clients) in parallel making full use of your CPU cores. Splitting the work up from one thread into multiple ones usually makes no sense in such an environment. As the system is already loaded and threading requires extra synchronization work etc. Usually it is better to offload "complex" tasks to backend system and then report back. By this the backend system doing the complex thing can be scaled independently. Also jobs can be queued so the user gets an instant report ("we're working") and later gets an report about being done.
Using pcntl_fork()
creates a copy of the process, this might be a complete copy of the webserver process which will try to both talk to the client over the same network connection (this is being copied too) leading to a complete mess. It will also lead to a mess with preexisting database connections etc. (at least both processes will try to close it ... so the second will receive a confused error by the database)
For actual threading there is the pthreads extension in PECL which doesn't create a copy of the process but shares the same process which messes with PHP's memory model (which assumes there is one request in one thread at a time which shares nothing with anybody else) some people use this, but offloading to other systems is typically better
What people often have are IO operations (i.e.. database calls) which take time. In that case asynchronous operations are the may to go (i.e. see mysqli_poll
and mysqli_reap_query
) this basically let's you do something on the CPU while waiting for IO and checking IO from time to time.
Upvotes: 1
Reputation: 1911
nothing "achieved" here, pcntl_fork
creates a new process.
If you create multiple processes you will most likely run into racing conditions at some point - PHP was not designed for parallel execution, you will have all kinds of weird errors and control-issues.
Using multiple processes within one program can also get really complicated - you will have to synchronize on mutexes and sequentialize algorithms which are dependent on each other.
Instead, you can write classes which call each others methods, that enables you to solve pretty much any problem - those which cant be solved without using fork
are most likely because of bad design; websites dont need to execute long-running tasks but they may QUEUE them, adding a crontab-entries would be one example.
Upvotes: 0