Jump to content
Gonzalez

[PHP] Thread Class

Recommended Posts

Posted
<?php
class Thread {
var $pref ; // process reference
var $pipes; // stdio
var $buffer; // output buffer

function Thread() {
$this->pref = 0;
$this->buffer = "";
$this->pipes = (array)NULL;
}

function Create ($file) {
$t = new Thread;
$descriptor = array (0 => array ("pipe", "r"), 1 => array ("pipe", "w"), 2 => array ("pipe", "w"));
$t->pref = proc_open ("php -q $file ", $descriptor, $t->pipes);
stream_set_blocking ($t->pipes[1], 0);
return $t;
}

function isActive () {
$this->buffer .= $this->listen();
$f = stream_get_meta_data ($this->pipes[1]);
return !$f["eof"];
}

function close () {
$r = proc_close ($this->pref);
$this->pref = NULL;
return $r;
}

function tell ($thought) {
fwrite ($this->pipes[0], $thought);
}

function listen () {
$buffer = $this->buffer;
$this->buffer = "";
while ($r = fgets ($this->pipes[1], 1024)) {
$buffer .= $r;
}
return $buffer;
}

function getError () {
$buffer = "";
while ($r = fgets ($this->pipes[2], 1024)) {
$buffer .= $r;
}
return $buffer;
}
}
?>

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.   Paste as plain text instead

  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...