Mutithreading in php / apache

I am creating a large PHP structure. Now we are trying to use all possible kernels in each script.

How to run a single script across multiple cores. For example, suppose I have two functions in one php file, which does a lot of processing. How can I run both simultaneously on two different processors, and then return the results to the script and continue with the rest of the processing.

Are there any other scripts that can be used to create such web applications ... I tried to watch online, but only the solutions I found were in desktop applications

+4
source share
3 answers

There is no such multiprocessor method. What you can do is create a main php file and then create a file that does something and then make a few ajax calls for this to open multiple threads for it. This is what I do. easy and not too difficult to set up

+1
source

You want to watch PCNTL . Keep in mind that it is designed for CGI-mod, but it can be used for apache.

Usage example:

<?php // Create the MySQL connection $db = mysql_connect($server, $username, $password); $pid = pcntl_fork(); if ( $pid == -1 ) { // Fork failed exit(1); } else if ( $pid ) { // We are the parent // Can no longer use $db because it will be closed by the child // Instead, make a new MySQL connection for ourselves to work with $db = mysql_connect($server, $username, $password, true); } else { // We are the child // Do something with the inherited connection here // It will get closed upon exit exit(0); ?> 
+1
source

You cannot use streams in php. these old posts may be helpful, but:

therefore, I assume that you will need to fork and synchronize using any of the ipc options, so this may be useful for an earlier message: How is the IPC between PHP clients and the Daemon C server?

0
source

All Articles