Class PowerProcess

Description

PowerProcess is an abstraction class for PHP's posix and pcntl extensions.

It enables easy process forking or threading to allow use of parallel processes for completing complicated tasks that would otherwise be inefficient for normal serial and procedural processing

If you like PowerProcess, please consider donating

  • BTC: 1K2tvdYzdDDd8w6vNHQgvbNQnhcHqLEadx
  • LTC: LfceD3QH2n1FqH8inqHdKxjBFV55QvuESv

  • author: Don Bauer <lordgnu@me.com>
  • version: 2.0
  • copyright:

    Copyright (c) 2011 Don Bauer <lordgnu@me.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  • link: https://github.com/lordgnu/PowerProcess
  • license: MIT

Located in /PowerProcess.class.php (line 43)


	
			
Variable Summary
static string $version
array $callbacks
boolean $complete
boolean $debugLogging
resource $logSocket
mixed $logTo
integer $maxThreads
array $myThreads
integer $parentPID
integer $parentSID
array $signalArray
mixed $threadData
integer $tickCount
boolean $timeStampLogs
Method Summary
object Instanced PowerProcess ([integer $maxThreads = 10], [integer $threadTimeLimit = 300], [boolean $daemon = false], [string $logTo = false], [boolean $debugLogging = false])
void __destruct ()
void CheckThreads ()
boolean ControlCheck ()
integer Daemonize ()
void Exec (string $process, [array $args = null])
integer GetControlPID ()
integer GetPID ()
array|boolean GetThreadStatus ([string|integer $name = false])
void InitializeLogger (string|boolean $logTo,  $debugLogging)
boolean IsDaemon ()
void KillThread ([integer $pid = 0])
void Log (string $msg, [boolean $internal = false])
integer PIDDead ([integer $pid = 0])
void RegisterCallback (int|string $signal, [callback $callback = false])
void RemoveLogger ()
void Restart ()
boolean RunControlCode ()
boolean RunThreadCode ()
void SendSignal ([integer $pid = 0], [integer $signal = 0])
void SetMaxThreads ([integer $maxThreads = 10])
void SetThreadTimeLimit ([integer $threadTimeLimit = 300])
void Shutdown ([boolean $exit = false])
void SignalDispatch (integer|string $signal)
boolean SpawnReady ()
boolean SpawnThread ([$name $name = false])
integer ThreadCount ()
void Tick ()
string WhoAmI ()
Variables
static string $version = '2.0' (line 49)

Current PowerProcess version

  • access: public
array $callbacks (line 70)

Callback array for setting callback functions based on signals that can be sent to the parent process

  • access: private
boolean $complete (line 63)

Boolean variable which determines whether or not to shutdown the control process (parent)

  • access: public
string $currentThread (line 77)

The name of the current thread. Used by WhoAmI()

  • access: private
boolean $debugLogging (line 84)

Whether to log internal debug message

  • access: private
resource $logSocket (line 159)

When logging is enabled, this points to the socket in which to write log messages.

  • access: private
mixed $logTo (line 152)

Location to log information messages to.

Can be a file or php://stdout, php://stderr. Set to false to disable

  • access: private
integer $maxThreads (line 95)

The maximum number of concurrent threads that can be running at any given time.

This setting has an impact on performance for PowerProcess so play with it on the system you are on to determine a good value. 10 is a good place to start

  • access: private
array $myThreads (line 102)

Array which stores the thread data for the control process (parent) to manage running child threads

  • access: private
integer $parentPID (line 119)

The pid of the parent process

Used after a process is forked to determine whether the new thread is to run the thread code

  • access: private
integer $parentSID (line 109)

Session ID of parent session when process is daemonized

  • access: private
array $signalArray = array(
SIGUSR1, // User-Defined 1
SIGUSR2 // User-Defined 2
)
(line 169)

Signals to install for SignalDispatcher.

You can use any signal constant PNCTL supports

mixed $threadData (line 56)

Data store for data that is to be passed to the child process which is to be spawned

  • access: public
integer $threadTimeLimit (line 142)

The maximum number of seconds a thread will be allowed to run.

Set to 0 to disable a time limit (use with caution)

  • access: private
integer $tickCount = 100 (line 126)

Sleep timer in micro seconds for the parent process to sleep between status checks using Tick()

  • access: private
boolean $timeStampLogs = true (line 133)

Whether to add a timestamp to log output

  • access: private
Methods
Constructor PowerProcess (line 187)

PowerProcess constructor.

Returns an instanced PowerProcess object or dies on failure

  • return: PowerProcess object
  • access: public
object Instanced PowerProcess ([integer $maxThreads = 10], [integer $threadTimeLimit = 300], [boolean $daemon = false], [string $logTo = false], [boolean $debugLogging = false])
  • integer $maxThreads: Max number of concurrent threads to allow at any given time
  • integer $threadTimeLimit: Maximum number of seconds a thread is allowed to live
  • boolean $daemon: Whether to start as a deamon or just a normal script
  • string $logTo: What stream to log output to
  • boolean $debugLogging: Whether to enable debug logging
Destructor __destruct (line 234)

Frees up memory

  • access: public
void __destruct ()
CheckThreads (line 537)

Checks all running threads to make sure they are still running and their time limit has not been exceeded

If a thread has exceeded it's time limit, this method will kill that process and dispatch the special signal 'threadotl'

  • access: private
void CheckThreads ()
ControlCheck (line 559)

Check if the current process is the control process

  • access: private
boolean ControlCheck ()
Daemonize (line 568)

Attempts to daemonize the current process

  • access: private
integer Daemonize ()
Exec (line 250)

Executes specified program in the current process space

  • access: public
void Exec (string $process, [array $args = null])
  • string $process: Path to the binary process to execute
  • array $args: Array of argument strings to pass to the program
GetControlPID (line 272)

Returns the PID of the process that spawned this one

  • access: public
integer GetControlPID ()
GetPID (line 263)

Returns the PID of the current process

  • access: public
integer GetPID ()
GetThreadStatus (line 282)

Get the status of a running thread by name or PID

  • access: public
array|boolean GetThreadStatus ([string|integer $name = false])
  • string|integer $name: The name or PID of the process for which you want status information
InitializeLogger (line 595)

Initialize the logging stream if enabled

  • access: private
void InitializeLogger (string|boolean $logTo,  $debugLogging)
  • string|boolean $logTo: The path or stream to log to or false to disable
  • $debugLogging
InstallSignalHandler (line 608)

Installs the default signal handlers

  • access: private
void InstallSignalHandler ()
IsDaemon (line 296)

Determine whether the control process is daemonized

  • access: public
boolean IsDaemon ()
KillThread (line 631)

Kill a thread by PID

  • access: private
void KillThread ([integer $pid = 0])
  • integer $pid: The PID of the thread to kill
Log (line 306)

Log a message

  • access: public
void Log (string $msg, [boolean $internal = false])
  • string $msg: The message to log
  • boolean $internal: Whether this is an internal debug logging message
PIDDead (line 643)

Determine whether a child pid has exited

Returns the PID of child which exited or 0

  • access: private
integer PIDDead ([integer $pid = 0])
  • integer $pid: The PID to check
RegisterCallback (line 355)

Registers a callback function for the signal dispatcher or for special signals used by PowerProcess

Special signals are:

  • 'shutdown' : Triggered on completion of the Shutdown() method
  • 'threadotl' : Triggered on killing a thread due to exceeding time limit

  • access: public
void RegisterCallback (int|string $signal, [callback $callback = false])
  • int|string $signal: The signal to register a callback for
  • callback $callback: The callback function
RemoveLogger (line 654)

Closes the logging stream

  • access: private
void RemoveLogger ()
Restart (line 321)

Restarts the control process

  • access: public
void Restart ()
RunControlCode (line 373)

Determines whether we should be running the control code or the thread code

  • access: public
boolean RunControlCode ()
RunThreadCode (line 388)

Determines whether we should be running the child code

  • access: public
boolean RunThreadCode ()
SendSignal (line 398)

Send a signal to a process

  • access: public
void SendSignal ([integer $pid = 0], [integer $signal = 0])
  • integer $pid
  • integer $signal
SetMaxThreads (line 411)

Set the max number of threads that can be running concurrently

  • access: public
void SetMaxThreads ([integer $maxThreads = 10])
  • integer $maxThreads: The max number of threads to run concurrently
SetThreadTimeLimit (line 420)

Set the max number of seconds a thread can run before being terminated

  • access: public
void SetThreadTimeLimit ([integer $threadTimeLimit = 300])
  • integer $threadTimeLimit: The max number of seconds a thread can run
Shutdown (line 429)

Initiates the shutdown procedure for PowerProcess

  • access: public
void Shutdown ([boolean $exit = false])
  • boolean $exit: When set to true, Shutdown causes the script to exit
SignalDispatch (line 665)

Handles dispatching of signals to user-defined callbacks

  • access: private
void SignalDispatch (integer|string $signal)
  • integer|string $signal
SpawnReady (line 448)

Determines if a new process can be spawned

  • access: public
boolean SpawnReady ()
SpawnThread (line 460)

Spawn a new thread

  • access: public
boolean SpawnThread ([$name $name = false])
  • $name $name: The name of the thread to be spawned
ThreadCount (line 503)

Get the count of running threads

  • access: public
integer ThreadCount ()
Tick (line 510)

Process signals to be dispatched and sleep for a number of microseconds

  • access: public
void Tick ()
WhoAmI (line 526)

Get the name of the current thread

  • return: The name of the current thread
  • access: public
string WhoAmI ()

Documentation generated on Wed, 11 Jan 2012 16:04:12 -0600 by phpDocumentor 1.4.3