Jump to content

Recommended Posts

i found this script to run many php as same time.

<?php
/**
 * @author Multi-threaded script by Nicolas Iglesias
 * @copyright 2008
 */
require_once ("thread_class.php");
$pathToPhp = "/usr/bin/php";
$A = new Threader("$pathToPhp -f test.php",null, "a");

/**
 * First argument: external script or command (required).
 * Second argument: arguments that your external script or command will receive (optional).
 * Third argument: name of thread (optional).
 */
$B = new Threader("$pathToPhp -f test.php",null, "b");
$C = new Threader("$pathToPhp -f test.php",null, "c");
$D = new Threader("$pathToPhp -f test.php",null, "d");
$E = new Threader("$pathToPhp -f test.php",null, "e");
$F = new Threader("$pathToPhp -f test.php",null, "f");

/**
 * We check if any of the 'active' states of our threads are true;
 * then we display its output using 'listen'.
 */
while ($A->active || $B->active || $C->active || $D->active || $E->active || $F->
    active)
{
    echo "[Thread ".$A->threadName."] => " . $A->listen();
    echo "[Thread ".$B->threadName."] => " . $B->listen();
    echo "[Thread ".$C->threadName."] => " . $C->listen();
    echo "[Thread ".$D->threadName."] => " . $D->listen();
    echo "[Thread ".$E->threadName."] => " . $E->listen();
    echo "[Thread ".$F->threadName."] => " . $F->listen();
}
?>
<?php
/**
 * Threader
 *
 * @package Threader Class
 * @author Nicolas Iglesias - nfiglesias@gmail.com - cleversight.com
 * @copyright 2008
 * @version 1.0
 * @access public
 */
class Threader
{
    var $threadName = null;
    var $rid = null;
    var $error = null;
    var $pipes = array();
    var $active = false;

  /**
   * Threader::Threader()
   *
   * The constructor which opens the process.
   *
   * @param mixed $cmd - Execute a shell command
   * @param mixed $vars - Pass arguments to shell command
   * @param string $name - Identifies your thread (useful for debug)
   * @return void
   */
    function Threader($cmd = null, $vars = null, $name = null)
    {
        $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"),
            2 => array("pipe", "w"));
        $pipes = array();
        if (!empty($cmd)):
        $this->threadName = $name;
        try
        {
            $this->rid = proc_open("$cmd $vars", $descriptorspec, $this->
                pipes, null, $_ENV);
            $this->active = true;
        }
        catch (exception $e)
        {
            $this->active = false;
            $this->error = $e->getMessage();
        }
        endif;
    }

  /**
   * Threader::listen()
   *
   * While $this->active == true, you can monitor a thread by calling this method.
   *
   * @return string - Will return the output of the running process
   */
    public function listen()
    {
        if (is_resource($this->rid) && !empty($this->pipes))
        {
            $stdout = (isset($this->pipes['1'])) ? $this->pipes['1'] : null;
            return fgets($stdout);
        }
        else
        {
            return null;
        }
    }

    function __destruct()
    {
        $this->active = false;
        if (is_resource($this->rid))
        proc_close($this->rid);
        if (!empty($this->error))
        echo $this->error;
    }
}
?>

but i dont understand this line in this script

$pathToPhp = "/usr/bin/php";
$A = new Threader("$pathToPhp -f test.php",null, "a");

can any one explain about $pathToPhp .

 

 

 

because script not working for me n i think something wrong with path.i am testing with linux web hosting..

 

so plz help me to shortout this .

Edited by ShivaGupta
Link to comment
https://forums.phpfreaks.com/topic/281803-need-help-with-multi-threaded-script/
Share on other sites


//path to php executable
$pathToPhp = "/usr/bin/php";

//$pathToPhp means run PHP
//-f test.php means run that file
//null means options you send to your script, in this case none
//a is any name you give to this thread so you can reference it later
$A = new Threader("$pathToPhp -f test.php", null, "a");

 

//path to php executable
$pathToPhp = "/usr/bin/php";

//$pathToPhp means run PHP
//-f test.php means run that file
//null means options you send to your script, in this case none
//a is any name you give to this thread so you can reference it later
$A = new Threader("$pathToPhp -f test.php", null, "a");

ok thank  you sir i understand that but here i have another quection n need help again..............

 

 

 

1.How to get correct "$pathToPhp  of my linux hosting ?

2. and for file this code can work with only test.php  or need full path of file ? 

 

1.How to get correct "$pathToPhp  of my linux hosting ?

which php or whereis php

 

2. and for file this code can work with only test.php  or need full path of file ?

 

Yes, the script is written to run some other commands from test.php file. Make sure they are on the same directory.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.