proc_open with ajax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andydrizen
Forum Newbie
Posts: 1
Joined: Sun Apr 12, 2009 9:50 am

proc_open with ajax

Post by andydrizen »

Hi all,

I've been using PHP for many years now, and have used proc_open, popen, exec and backticks sparsely before, but this next project has me stumped.

I want to make an interactive GAP (mathematics program) session using AJAX. I thought I could do it in the following way:

1. initiate the terminal

2. check for new commands using AJAX and, if it finds any, then execute the commands.

The problem is that the pipe has to close before outputting anything: here is my code with the red section being the problematic bit.

Code: Select all

$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", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
 
$process = proc_open('gap', $descriptorspec, $pipes, NULL, NULL);
if (is_resource($process)) {
[b][color=#FF0000]  fwrite($pipes[0], '3+4;');[/color][/b]
    [b][color=#FF0000] fclose($pipes[0]);[/color][/b]
  [b][color=#FF0000]   echo "<pre>".stream_get_contents($pipes[1])."</pre>";[/color][/b]
    fclose($pipes[1]);
}
Is there any way of outputting without closing the pipe, or getting back in to the pipe once it's closed?

Many thanks,
A
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: proc_open with ajax

Post by php_east »

i was told php is single threaded, so unfortunately when the script ends, the thread ends and so will all child processes including the ones we opened with proc_open.
Post Reply