PHP: Process Control Functions

I had quite forgotten that PHP can fork a process just like in C! This might be a useful thing to know for doing background tasks, but to be honest the extra complexity required will probably put me off doing it. That’s where this Thread class comes in handy..

<?php
include ("threadClass.php");

class testThread extends Thread {

  function testThread($name) {
    $this->Thread($name); // calls the parent constructor and assign its name
  }

  function run() {
    while(true) {
      sleep(1);
      print time() ."-" . $this->getName() . " said ok...\n"; // every second we're going to print this line...
    }
  }
}
// Main program. Bring up two instances of the same class (testThread).
// They runs concurrently. It's a multi-thread app with a few lines of code!!!

$test1 = new testThread ("Thread-1");
$test2 = new testThread ("Thread-2");
$test1->start();
$test2->start();

print "This is the main process. Press [CTRL-CANC] to terminate.\n";
while(true) {sleep(1);}

?>


1 Comment

BDKR (4 comments.) on May 26, 2003 at 10:41 pm.

I posted about the same topic a couple of days ago. It’s a great class. As soon as I get over some of the routing projects I got goin’ on, I plan on messing with it a little.

Cheers,
BDKR

Reply

Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Holy Shmoly! is Stephen Fry proof thanks to caching by WP Super Cache