1

I am trying to figure out how to run a parallel execution of a script on the same server. As it is now I am using cUrl, but am not sure this is the best or smartest way to do so. I want, when my primary php script is running, to be able to execute another script with data from primary script but without the primary script to be waiting on the other script,as if I included it. I am prohibited to use javascript or jQuery to run some ajax, so what i have come up with was the cUrl solution... But is this the right way to do so when the 2 scripts are on the same server?

Here is an example of my curl:

$data = array("username" => "$get_username"
             ,"password" => "$md5_pass"
             ,"sqlId" => (int)$userid
             ,"appversion" => (int)$get_version
             ,"proversion" => (int)0
             );

$url = "http://myserver.com/secondscript.php";

$ch = curl_init( $url );
# Setup request to send json via POST.
$payload = json_encode( array("action" => "newUser", "player" => $userid, "json" => $data ));
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch); 

Hoping for help or/and guidance in this matter.

Thanks in advance :-)

Mansa
  • 2,277
  • 10
  • 37
  • 67
  • If it's a local script you can also run it with shell_exec as a background process (append the `&` argument to the command). With curl, setting CURLOPT_TIMEOUT to a very low value should prevent your script from waiting for a response for too long – nice ass Mar 22 '14 at 11:17
  • I will look into shell_exec but don't have much experience with this. Is the shell_exec method better for server load than the curl? or is it the same? – Mansa Mar 22 '14 at 11:21
  • Theoretically it should be faster to execute it directly, because curl has to do a domain lookup. Related: http://stackoverflow.com/questions/45953/php-execute-a-background-process – nice ass Mar 22 '14 at 11:30
  • Thanks... BTW. Is it possible to send data/json through shell_exec? – Mansa Mar 22 '14 at 11:36
  • yes, as command line arguments (read http://www.php.net/manual/en/features.commandline.php) – nice ass Mar 22 '14 at 11:50
  • Thanks... This may bit a bit over my head... Not sure how to use/ or do so, but thanks... :-) – Mansa Mar 22 '14 at 12:11

1 Answers1

0

I had a similar challenge. I made a little php demo to show real parallel processing on a UNIX system (Ubuntu and PHP 5.3.2)...

  • using real UNIX parallel processes
  • limiting parallization (configurable)
  • limiting execution time on a system level
  • using proc_* php commands (proc_open, proc_terminate, ...)

Here is my playground that show all the stuff: Github-Repo: PHP-Process-Control

The demo starts defining the PHP scripts to be executed:

$manager = new ProcessManager();
$manager->addScript("doit.php 1", 5);
$manager->addScript("doit.php 2", 10);
$manager->addScript("doit.php 3", 35);
$manager->exec();

Maybe you can use parts or some ideas to execute your scripts in parallel.

WeSee
  • 3,158
  • 2
  • 30
  • 58