Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running the same script at the same time and PHP's single-threaded status

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?

like image 994
Brett Avatar asked Oct 19 '13 20:10

Brett


People also ask

Can a process be single threaded and multithreaded?

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.

What can occur if two threads are executing 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.

What is the difference between single threaded and multithreaded process?

"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.

Can threads be executed in parallel?

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.


1 Answers

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:

  • spawn a php interpreter process (cgi - generic CGI gateway)
  • manipulate an in process instance of the interpreter (mod_php5 - apache specific PHP module, services both mpm and worker installations of Apache)
  • communicate with an FCGI process (php-fpm - commonly used with nginx, some apache usage goes on still ...)

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 ?

like image 92
Joe Watkins Avatar answered Sep 21 '22 18:09

Joe Watkins