0
$a=new FileProcessing($path1);
$b=new FileProcessing($path2); 
$a->doProcessFile();
$b->doProcessFile();

this code run doProcessFile() for $a then run doProcessFile() for $b in sequential order. I want run doProcessFile() for $a in parallel with doProcessFile() for $b so I can process different file in parralel. I can do that in PHP ?

As I can see, PHP process script in sequential order only. Now I wonder if I can run multiple instances of the same script in parralel ,the difference between running scripts is a parameter I pass when I call each script. for example : php myscript.php [path1] then I call this script a second time with different parameter.

hicham
  • 25
  • 1
  • 3
  • 4
    No. PHP is not a multi-threaded language. Your only recourse is to `exec()` or otherwise spawn two SEPARATE instances of php, each doing their separate path. – Marc B Jul 22 '13 at 21:04
  • 1
    http://stackoverflow.com/questions/14236296/asynchronous-function-call-in-php – castis Jul 22 '13 at 21:05
  • Like @MarcB mentioned PHP is not multi-threaded. Besides using exec you could also look into solutions like beanstalkD http://kr.github.io/beanstalkd/ – tlenss Jul 22 '13 at 21:10
  • @tlenss - php isn't multithreaded? www.php.net/Thread disagrees with you :/ – N.B. Jul 22 '13 at 23:13
  • @N.B.pthreads is not part of the core. So the answer remains. However it's a viable option here. – tlenss Jul 23 '13 at 05:51

3 Answers3

0

You can use pcntl_fork, but only when running PHP from command line instead of as a web server.

Alternately, you can use gearman.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
Evgeny
  • 66
  • 4
0

Never tried that myself but in theory should be possible:

  1. Convert your doProcessingFile method to a standalone PHP script process_file.php that takes file to process as input.

  2. Put the files for processing to a folder created for that sole purpose

  3. Create a shell script paralellizer that will list the folder with files to process and send the files for parallel processing (note that it accepts number of files to process as arg - this bit could maybe be done nicer as a part of oneliner below):

     #!/bin/bash
    
     ls special_folder | xargs -P $1 -n 1 php process_file.php
    
  4. Call parallelizer from php and make sure process_file returns processing result status :

     $files_to_process = Array($path1, $path2); 
     exec('parallelizer '.count($files_to_process), $output);
     // check output
     // access processed files  
    

disclaimer: just a rough ugly sketch of idea. I'm sure it can be improved

Jarek.D
  • 1,274
  • 1
  • 8
  • 18
0

make 2 files

1. processor.php

<?php
if($arc < 1) user_error('needs a argument');
$a=new FileProcessing($argv[1]);
$a->doProcessFile();

2. caller.php

<?php
function process($path,$name){
  $a = array();
  $descriptorspec = array(
    1 => array("file", "/tmp/$name-output.txt", "a"),  // stdout is a pipe that the child will write to
    2 => array("file", "/tmp/$name-error.txt", "a") // stderr is a file to write to
  );
  return proc_open(PHP_BINARY.' processor.php '.escapeshellarg($path), $descriptorspec, $a);
}
$proc1 = process($path1,'path1');
$proc2 = process($path2,'path2');
//now wait for them to complete
if(proc_close($proc1))user_error('proc1 returned non 0');
if(proc_close($proc2))user_error('proc2 returned non 0');
echo 'All done!!';

be sure to read the documentation of proc_open to for more information, also note that you need to include the class(and anything it needs) in the processor because its a new php environment

borrel
  • 911
  • 9
  • 17