Executing MPI commands using PHP - php

I am trying to execute a mpi program using php as I have to provide a web-interface to user.PHP successfully executes the command and return output only If I have only one process, i.e
$output = system(" mpiexec -hostfile /data/hosts -np 1 /data/./hello",$returnValue);
But I have need more then one process and I have tried following ways but results are same i.e No response from the mpi program.
using system ()
$output = system(" mpiexec -hostfile /data/hosts -np 2 /data/./hello",$returnValue);
using shell_exec ()
$output = shell_exec(" mpiexec -hostfile /data/hosts -np 2 /data/./hello");
If I use these methodes to run a simple c program I do receive response.
$output = system("/data./hello",$returnValue);
Please assist me. Many thanks.

Problem seem to be that you are trying to store the output of "system()" into "$output" while it's already storing the value on "$returnValue". Try this:
exec('mpiexec -hostfile /data/hosts -np 2 /data/./hello', $var);
var_dump($var);
For some odd reason PHP doesn't get along with multi threads.
A dirty workarround will be to output the result to a file and feed php from that filem, something like:
system(" mpiexec -hostfile /data/hosts -np 2 /data/./hello > myfile.txt 2>&1");
$handle = file_get_contents('myfile.txt');

Related

How to get data from Python over to PHP

What are the possible methods to do this?
When I either open a page or click a button on my PHP website, it runs a python program that returns some data (maybe a string) back to PHP.
It seems people keep saying to use exec or shell exec but I tested this on WAMP, didn't work for me(maybe I did something wrong).
Is there a method I could do something like a API but not to access database but to just run a program?
EDIT: From advice on comments, I was able to get something.
exec('python get.py', $output);
var_dump($output);
using above, I got output.
C:\wamp64\www\ai\get.php:10:
array (size=0)
empty
my python code is just a print statement that says hello world. do I need something for python side?
EDIT TWO: OMFG FINALLLUUYY GOT IT
$cmd = "C:\Users\Jack\AppData\Local\Programs\Python\Python36\python.exe get.py";
exec($cmd, $output);
var_dump($output);

PHP shell_exec not producing desired output

I'm trying to run a java program and return the output then use that on my page.
if ($program) {
$output = shell_exec("java ftoa 12");
var_dump($output);
}
For some reason, that var_dump puts out null. I have ftoa.class in the same directory by the way. Also, the server my school is using runs an older version of php, not sure which one. Also when I run the command from the php command line, it works fine.
$:php -a
php>$output = shell_exec("java ftoa 12");
php>echo $output;
php>(desired output)

retrieve R output in PHP

Here's the issue:
I am using R to run some statistical analysis. The results of which will eventually be sent to a an embedded swf on the user's client machine.
To do this, I have PHP execute a shell script to run the R program, and I want to retrieve the results of that program so I can parse them in PHP and respond with the appropriate data.
So, it's simply:
$output = shell_exec("R CMD BATCH /home/bitnami/r_script.R");
echo $output;
But, I receive nothing of course, because R CMD BATCH writes to a file. I've tried redirecting the output in a manner similar to this question which changes my script to
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R /dev/tty');
echo $output;
But what I get on the console is a huge spillout of the source code, and nothing is echoed.
I've also tried this question's solution in my R script.
tl;dr; I need to retrieve the results of an R script in PHP.
Cheers!
If it writes to file perhaps you could use file_get_contents to read it?
http://php.net/manual/en/function.file-get-contents.php
Found it, the answer is through Rscript. Rscript should be included in the latest install of R.
Using my code as an example, I would enter this at the very top of r_script.R
#!/usr/bin/Rscript --options-you-need
This should be the path to your Rscript executable. This can be found easily by typing
which Rscript
in the terminal. Where I have --options-you-need, place the options you would normally have when doing the CMD BATCH, such as --slave to remove extraneous output.
You should now be able to run your script like so:
./r_script.R arg1 arg2
Important! If you get the error
Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
could not find function "is"
You need to include the "methods" package, like so:
require("methods");
Perhaps,a much simpler workaround, would be:
$output = shell_exec('R CMD BATCH /home/bitnami/raschPL.R > /dev/tty 2>&1');
echo $output;
Redirects both STDOUT and STDERR, since R outputs to STDERR, by default.

PHP readline() when STDIN other than keyboard

I'm writing an script that is able to read from stdin and then request for confirmation.
<?php
$stream = fopen('php://stdin', 'r');
$input = fgets($stream, 1024);
$confirmation = readline('Are you sure?');
if ( $confirmation == 'y' )
/* Do dangerous stuff */
When I run it directly:
$ php script.php
inputdata
^D
Are you sure?
But I'm trying to run it using a file as STDIN. In that case, readline() returns false and no confirmation is prompted.
$ php script.php < data.txt
or
$ echo "foobar" | php script.php
How can I read both from the STDIN and keyboard when invoking this script in this way?
Thanks.
Use fgetc function with STDIN. See example bellow.
$input = fgets(STDIN, 1024);
echo "\nAre you sure?\n";
$conf = fgetc(STDIN);
if($conf=='y'){
echo "Great! Lets go ahead\n";
}else{
echo "Okay, May be next time\n";
}
Console output
Sample 1
$ echo 'data
> y
> ' | php php_readline.php
Are you sure?
Great! Lets go ahead
Sample 2
$ php php_readline.php
Some data
Are you sure?
n
Okay, May be next time
According to a commenter ont he PHP page ( http://php.net/manual/en/book.readline.php ):
When readline is enabled, php switches the terminal mode to accept
line-buffered input. This means that the proper way to use the cli
when you pipe to an interactive command is to explicitly specify that
php is not using the terminal for input:
php somescript.php < /dev/null | less
I believe the point is to append | less. Not knowing the structure of your data, presumably something might need to be done within your script to handle the transition from data to confirmation.
One might add a mechanism to detect when terminal line-buffered input is enabled.

Get PHP to update with every line returned from a shell command

Background:
I'm trying to write a shell script using php that will automatically checkout a couple of large SVN repos. I am also using the PEAR console progress bar class to display the progress of the checkout (not totally necessary, but the thing that prompted my question).
Question:
Is there a way to run a loop that will update with every line output to STDIN on the commandline?
If I do
<?php shell_exec("svn co svn://my.repo.com/repo/trunk"); ?>
I get back all of the output from the command in a giant string.
Trying a loop like
<?php $bar = new Console_ProgressBar('%fraction% [%bar%] %percent% | %elapsed% :: %estimate%', '=>', ' ', 80, $total);
$bar->display(0);
stream_set_blocking(STDIN, 0);
$output = array();
$return = '';
exec("svn co $svnUrl $folder", $output, $return);
while (!isset($return))
{
$bar->update(count($output));
}
$bar->erase(); ?>
will display the bar but not update.
Any solutions?
========================= UPDATE =======================================
Here's the working code I came up with based on the answer (for future reference):
<?php
$bar = new Console_ProgressBar('%fraction% [%bar%] %percent% | %elapsed% :: %estimate%', '=>', ' ', 80, $total);
$handle = popen("/usr/bin/svn co $svnUrl $folder", 'r');
$elapsed = 0;
$bar->display($elapsed);
while ($line = fgets($handle))
{
$elapsed++;
$bar->update($elapsed);
}
pclose($handle);
?>
PHP is not multi-threaded. exec() and company will block your script until the exec'd task completes. There's no point in using the while() loop, because the loop will not even start running until the svn call has completed.
If you want to run multiple parallel svn's, you'll need to use popen() (or proc_open()), which gives you a filehandle from which you can read the external command's output, and have multiple such external commands at the same time.
Use Output Buffering
see:
http://php.net/manual/en/function.ob-start.php
http://php.net/manual/en/function.ob-flush.php
http://php.net/manual/en/function.ob-end-clean.php
and see some related:
PHP Output buffering on the command line
Kohana 3 Command line output buffering?
AS I understand your problem that php execute script synchronously. Thats mean cycle will be run only after exec command and output will not be changed because it already done. Try to use fork.

Categories