Anush
Anush

Reputation: 1050

mutithreading in php/apache

I am building a large php framework. Now we are trying to utilise the all possible cores in each script.

How can be run one script across multiple cores. For example lets say I have two functions on one php file which do hefty processing. How can I run both at the same time 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 web applications like this...I have tried looking on-line but only solutions I have found were in desktop applications

Upvotes: 4

Views: 1228

Answers (3)

Vish
Vish

Reputation: 4492

There is no such multiprocessing method. What you may do is create a main php file and then have a file that does something and then make multiple ajax calls to that to open multiple threads for it. Thats what I do. easy and not too complex to setup

Upvotes: 1

marcelog
marcelog

Reputation: 7180

you cant use threads in php. this older posts might be useful, though:

so i guess you will need to fork and synchronize via any of the ipc options, so this also older post might be helpful: How to IPC between PHP clients and a C Daemon Server?

Upvotes: 0

SeanCannon
SeanCannon

Reputation: 77956

You want to look into PCNTL. Keep in mind it's designed for the CGI-mod but it can be used for apache.

Example usage:

<?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); 
?> 

Upvotes: 1

Related Questions