Re: child process

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

Thanks for your help, but i am not able to understand this code properly.
What is the use for STDIN and STDOUT? How data is coming to STDIN? Can you
help me understanding this?

Main Code :
// Build file descriptor list for talking to child process
        $stdErr = Mage::getBaseDir('log') .
'/fontis_feedsgenerator_subprocess.txt';
        $descriptorSpec = array(
            0 => array("pipe", "r"),  // stdin is a pipe that the child
will read from
            1 => array("pipe", "w"),   // stdout is a pipe that the child
will write to
            2 => array("file", $stdErr, "a") // stderr is a file to write to
        );
        $pipes = array();

        // Build child path from current file location and Magento path
delimiter
        $execPath = escapeshellarg(dirname(__FILE__) . DS .
$this->CHILD_SCRIPT);

        // Open child process with proc_open
        //$this->log("Opening child: ".$exec_path);
        $process = proc_open('php '.$execPath, $descriptorSpec, $pipes);

        if (!is_resource($process)) {
            $this->log("Error opening child process.");
            fclose($pipes[0]);
        } else {
            // Write entity id/attribute info to pipe
            $data = array(
                "magento_path" => $this->info("base_dir"),
                "store_id" => $this->info("store_id"),
                "child_class" =>
Mage::getConfig()->getModelClassName($this->_childClass),
                "entity_ids" => $this->_accumulator,
                "attributes" => $this->_attributes,
                "generate_categories" => $this->generateCategories,
                "config_path" => $this->_configPath,
            );
            $this->_accumulator = array();

            fwrite($pipes[0], json_encode($data));
            fclose($pipes[0]);

            // Read child's output until it finishes or child is no longer
running
            $result = '';
            do {
                $result .= fgets($pipes[1]);
                $state = proc_get_status($process);
            } while (!feof($pipes[1]) && $state['running']);

            // read JSON-encoded data from child process
            $batchData = json_decode($result, true);

            if (is_array($batchData)) {
                $this->populateFeedWithBatchData($batchData);
            } else {
                $this->log("Could not unserialize returned data from child
to array:");
                $this->log($result);
                $this->log($batchData);
            }

           // Close child process if it's still open
           $status = proc_close($process);
        }

        // Close remaining file handles
        @fclose($pipes[1]);


PHP code in file that is called using proc_open
$input = '';
do {
    $input .= fgets(STDIN);
} while (!feof(STDIN));

$config = json_decode($input);

// Start-up Magento stack
require_once $config->magento_path . '/app/Mage.php';

$_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = $config->magento_path .
        '/app/code/community/' . str_replace('_', '/',
$config->child_class) . '.php';

$storeId = $config->store_id;
Mage::app($storeId);

$class = $config->child_class;
$child = new $class($config);
$child->produceBatch();

Function produceBatch :

$products = array();
        foreach ($this->config->entity_ids as $entity_id) {
            $this->log("Processing $entity_id");
            $product = Mage::getModel('catalog/product')->load($entity_id);
            $products = array_merge($products,
$this->processProduct($product));
        }
        fwrite(STDOUT, json_encode($products));

Thanks,
Nupur Walia

On Fri, Jan 16, 2015 at 3:23 PM, Stuart Dallas <stuart@xxxxxxxx> wrote:

>  Please include the list when replying, or let me know where to send the
> invoice. And please don’t top-post; it’s against the rules not to mention
> incredibly annoying:
> http://git.php.net/?p=php-src.git;a=blob_plain;f=README.MAILINGLIST_RULES;hb=HEAD
>
> On Friday, Jan 16, 2015 at 9:43 am, nupur walia <nupur.wl@xxxxxxxxx>,
> wrote:
>
>>  Hi,
>>
>> I am trying to run a php process like 'php' path to php file. Its a
>> magento extension that used child processes to create product feeds. My
>> code is :
>> $stdErr = Mage::getBaseDir('log') .
>> '/fontis_feedsgenerator_subprocess.txt';
>>         $descriptorSpec = array(
>>             0 => array("pipe", "r"),  // stdin is a pipe that the child
>> will read from
>>             1 => array("pipe", "w"),   // stdout is a pipe that the child
>> will write to
>>             2 => array("file", $stdErr, "a") // stderr is a file to write
>> to
>>         );
>>         $pipes = array();
>>
>>         // Build child path from current file location and Magento path
>> delimiter
>>         $execPath = escapeshellarg(dirname(__FILE__) . DS .
>> $this->CHILD_SCRIPT);
>>
>>         // Open child process with proc_open
>>         //$this->log("Opening child: ".$exec_path);
>>         $process = proc_open('php '.$execPath, $descriptorSpec, $pipes);
>>
>>         if (!is_resource($process)) {
>>             $this->log("Error opening child process.");
>>             fclose($pipes[0]);
>>         } else {
>>             // Write entity id/attribute info to pipe
>>             $data = array(
>>                 "magento_path" => $this->info("base_dir"),
>>                 "store_id" => $this->info("store_id"),
>>                 "child_class" =>
>> Mage::getConfig()->getModelClassName($this->_childClass),
>>                 "entity_ids" => $this->_accumulator,
>>                 "attributes" => $this->_attributes,
>>                 "generate_categories" => $this->generateCategories,
>>                 "config_path" => $this->_configPath,
>>             );
>>             $this->_accumulator = array();
>>
>>             fwrite($pipes[0], json_encode($data));
>>             fclose($pipes[0]);
>>
>>             // Read child's output until it finishes or child is no
>> longer running
>>             $result = '';
>>             do {
>>                 $result .= fgets($pipes[1]);
>>                 $state = proc_get_status($process);
>>             } while (!feof($pipes[1]) && $state['running']);
>>
>>             // read JSON-encoded data from child process
>>             $batchData = json_decode($result, true);
>>
>>             if (is_array($batchData)) {
>>                 $this->populateFeedWithBatchData($batchData);
>>             } else {
>>                 $this->log("Could not unserialize returned data from
>> child to array:");
>>                 $this->log($result);
>>                 $this->log($batchData);
>>             }
>>
>>            // Close child process if it's still open
>>            $status = proc_close($process);
>>         }
>>
>>         // Close remaining file handles
>>         @fclose($pipes[1]);
>>
>>
>> It read data and write to it some file. Using proc_open pipes. I dont
>> know how to use curl for this code?
>>
>
> Curl is not really applicable to what you’re doing.
>
> You’re wanting to run PHP code from PHP code? There’s no need to run a new
> process for this, simply include the code you want to run. How easy that
> will be depends on what the PHP script you’re including is trying to do,
> but there is rarely a reason to run a new instance of PHP from a PHP script.
>
> -Stuart
>
> --
> Stuart Dallas
> 3ft9 Ltd
> http://3ft9.com/
>
>   On Fri, Jan 16, 2015 at 3:07 PM, Stuart Dallas <stuart@xxxxxxxx> wrote:
>>
>>>   On Friday, Jan 16, 2015 at 9:15 am, nupur walia <nupur.wl@xxxxxxxxx>,
>>> wrote:
>>>
>>>> I am using proc_open in my code but this function is disabled on my
>>>> server.
>>>> Can you please tell me if there is any other function that i can use in
>>>> place of proc_open?
>>>>
>>>> Can i use curl in place of proc_open? If yes then please let me know how
>>>> can i use curl to replace proc_open?
>>>
>>>
>>>  What are you actually trying to do?
>>>
>>> If you’re trying to make an HTTP request (as implied by your question)
>>> you can use the curl extension if it’s installed: http://php.net/curl.
>>> If not there are probably other way to accomplish what you’re trying to do,
>>> but we’ll need details to be able to help any further.
>>>
>>> -Stuart
>>>
>>> --
>>> Stuart Dallas
>>> 3ft9 Ltd
>>> http://3ft9.com/
>>>
>>
>>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux