2017 © Pedro Peláez
 

library libsystem-thread

System Threads manage for PHP

image

rgnu/libsystem-thread

System Threads manage for PHP

  • Friday, March 18, 2016
  • by garciarodrigor
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

php-libsystem-thread

System Thread fork and run code in sub-processes., (*1)

<?php
require_once __DIR__ . '/vendor/autoload.php';

class SleepThread extends System_Thread
{
    private $sleep;

    public function __construct($sleep=3) {
        $this->sleep = $sleep;
    }

    public function run() {
        while ($this->sleep && !$this->isTerminated()) {
            print "Sleep:".$this->sleep."\n";
            $this->sleep -= 1;
            sleep(1);
        };
    }
}


class ThreadListener implements Event_Observer
{
    public function update(Event_Observed $obj)
    {
        printf("%s > %s activeCount=%d\n", strftime("%Y-%m-%d %H:%M:%S", time()), "$obj", $obj->activeCount());
    }
}

class System_Thread_ManagerStrategy_Constant extends System_Thread_ManagerStrategy
{
    public function __construct($number)
    {
        $this->number = $number;
    }

    public function manage(System_Thread_Manager $manager)
    {
        return $this->number;
    }
}


$l = new ThreadListener();

$t1 = new SleepThread(4);
$t1->attach($l);

$t2 = new SleepThread(2);
$t2->attach($l);

$g = new System_Thread_Group();
$g->addThread($t1);
$g->addThread($t2);
$g->attach($l);

$m1 = new System_Thread_Manager($t1, new System_Thread_ManagerStrategy_Constant(6));
$m1->attach($l);

$m2 = new System_Thread_Manager($t2, new System_Thread_ManagerStrategy_Constant(2));
$m2->attach($l);

$mg = new System_Thread_Group();
$mg->addThread($m1);
$mg->addThread($m2);
$mg->attach($l);

$th = $mg;

pcntl_signal(SIGTERM, array($th, 'stop'));
pcntl_signal(SIGINT,  array($th, 'stop'));

$th->start();

while($th->isAlive()) { 
    echo "Active Thread " . $th->activeCount() . "\n";
    sleep(1);
}

The Versions

18/03 2016

dev-master

9999999-dev

System Threads manage for PHP

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0

 

The Development Requires

by Rodrigo Garcia

threads rgnu

08/11 2013

v1.0.2

1.0.2.0

System Threads manage for PHP

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0

 

The Development Requires

by Rodrigo Garcia

threads rgnu

07/11 2013

1.0.1

1.0.1.0

System Threads manage for PHP

  Sources   Download

LGPL-2.1

The Requires

  • php >=5.3.0

 

The Development Requires

by Rodrigo Garcia

threads rgnu