removed checking function existence to support static class methods
This commit is contained in:
parent
2becb1d28a
commit
ac2a584b8d
|
@ -1,4 +1,6 @@
|
||||||
php-thread
|
php-thread
|
||||||
==========
|
==========
|
||||||
|
|
||||||
Simple implementation of threading in PHP using pnctl
|
* Thread.php: Simple implementation of threading in PHP using pnctl
|
||||||
|
* ThreadQueue.php: Enqueue tasks to run them in parallel
|
||||||
|
|
||||||
|
|
36
Thread.php
36
Thread.php
|
@ -34,11 +34,8 @@ class Thread
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $_errors = array(
|
private $_errors = array(
|
||||||
Thread::FUNCTION_NOT_CALLABLE =>
|
Thread::FUNCTION_NOT_CALLABLE => 'You must specify a valid function name that can be called from the current scope.',
|
||||||
'You must specify a valid function name that can be called '.
|
Thread::COULD_NOT_FORK => 'pcntl_fork() returned a status of -1. No new process was created'
|
||||||
'from the current scope.',
|
|
||||||
Thread::COULD_NOT_FORK =>
|
|
||||||
'pcntl_fork() returned a status of -1. No new process was created'
|
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,6 +53,17 @@ class Thread
|
||||||
*/
|
*/
|
||||||
private $_pid;
|
private $_pid;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exits with error
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
|
||||||
|
private function fatalError($errorCode){
|
||||||
|
throw new Exception( $this->getError($errorCode) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if threading is supported by the current PHP configuration
|
* Checks if threading is supported by the current PHP configuration
|
||||||
*
|
*
|
||||||
|
@ -84,6 +92,7 @@ class Thread
|
||||||
*/
|
*/
|
||||||
public function __construct( $runnable = null )
|
public function __construct( $runnable = null )
|
||||||
{
|
{
|
||||||
|
if(!Thread::isAvailable)throw new Exception("Threads not supported");
|
||||||
if ( $runnable !== null ) {
|
if ( $runnable !== null ) {
|
||||||
$this->setRunnable($runnable);
|
$this->setRunnable($runnable);
|
||||||
}
|
}
|
||||||
|
@ -101,10 +110,7 @@ class Thread
|
||||||
if ( self::isRunnableOk($runnable) ) {
|
if ( self::isRunnableOk($runnable) ) {
|
||||||
$this->runnable = $runnable;
|
$this->runnable = $runnable;
|
||||||
} else {
|
} else {
|
||||||
throw new Exception(
|
$this->fatalError(Thread::FUNCTION_NOT_CALLABLE);
|
||||||
$this->getError(Thread::FUNCTION_NOT_CALLABLE),
|
|
||||||
Thread::FUNCTION_NOT_CALLABLE
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,8 +126,7 @@ class Thread
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the callback is ok (the function/method
|
* Checks if the callback is ok (the function/method
|
||||||
* actually exists and is runnable from the current
|
* is runnable from the current context)
|
||||||
* context)
|
|
||||||
*
|
*
|
||||||
* can be called statically
|
* can be called statically
|
||||||
*
|
*
|
||||||
|
@ -131,7 +136,7 @@ class Thread
|
||||||
*/
|
*/
|
||||||
public static function isRunnableOk( $runnable )
|
public static function isRunnableOk( $runnable )
|
||||||
{
|
{
|
||||||
return ( function_exists($runnable) && is_callable($runnable) );
|
return ( is_callable($runnable) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -166,10 +171,7 @@ class Thread
|
||||||
{
|
{
|
||||||
$pid = @pcntl_fork();
|
$pid = @pcntl_fork();
|
||||||
if ( $pid == -1 ) {
|
if ( $pid == -1 ) {
|
||||||
throw new Exception(
|
$this->fatalError(Thread::COULD_NOT_FORK);
|
||||||
$this->getError(Thread::COULD_NOT_FORK),
|
|
||||||
Thread::COULD_NOT_FORK
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if ( $pid ) {
|
if ( $pid ) {
|
||||||
// parent
|
// parent
|
||||||
|
@ -252,4 +254,4 @@ class Thread
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
- See more at: http://blog.motane.lu/2009/01/02/multithreading-in-php/#sthash.JevFlLcL.dpuf
|
|
||||||
|
|
Loading…
Reference in a new issue