I know PHP is single-threaded, so I am wondering, does this mean if I run say foo.php
in a browser tab and then open another browser tab and run the same file, will it still run if foo.php
is still running in the other browser tab!?
If not, how can you run the same script at once? Do you just rename the script something else and then you can run both at the same time? ...or am I getting confused here?
Single threaded processes contain the execution of instructions in a single sequence. In other words, one command is processes at a time. The opposite of single threaded processes are multithreaded processes. These processes allow the execution of multiple parts of a program at the same time.
Parallel Processing: When multi-threaded program execution occurs on a multiple core system (multiple uP, or multiple multi-core uP) threads can run concurrently, or in parallel as different threads may be split off to separate cores to share the workload. This is one example of parallel processing.
"Single-threaded" means that we open a single connection and measure the speeds from that. "Multi-threaded" means that we're using multiple connections - usually anywhere from 3 to 8 - at the same time, and measure the total speed across them all.
On a system with more than one processor or CPU cores (as is common with modern processors), multiple processes or threads can be executed in parallel.
PHP is NOT single threaded by nature.
http://php.net/Thread
It is, however, the case that the most common installation of PHP on unix systems is a single threaded setup, as is the most common Apache installation, and nginx doesn't have a thread based architecture whatever.
In this most common unix setup, each http server process accepting requests can do one of a few things:
In the most common Windows setup and some more advanced unix setups, PHP can and does operate multiple interpreter threads in one process.
In any case, each instance of the interpreter, be it in another process or in another thread, has access to a unique representation of the script that is a result of compiling the PHP script into opcodes for execution by Zend.
This is by design, it's referred to as a shared nothing architecture and it is the thing allowing you to create hundreds/thousands/millions of instances of the same script that for the most part do not interfere with each other.
Even where PHP utilizes opcode caching to save resources on the compilation stage of execution, no two instances of the interpreter access the same physical memory.
Noteworthy: PHP is not completely void of the effects of concurrent execution in any setup, this is the nature of modern computing; for example if your script edits a text file, and 1000 clients come at once, what do you think will happen to the text file ?
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