Threads in PHP

PHP: Process Control Functions support in PHP implements the Unix style of process creation, program execution, signal handling and process termination. Process Control should not be enabled within a web server environment and unexpected results may happen if any Process Control functions are used within a web server environment.
UNIX only

Class: Thread (seems o be the same as Pear::PHP_Fork package) is a wrapper around the pcntl_fork() stuff, with a API set like Java language.
Practical usage is done by extending this class, and re-defining the run() method.
This way PHP developers can enclose logic into a class that extends PHP_Fork, then execute the start() method that forks a child process.
Communications with the forked process is ensured by using a Shared Memory Segment; by using a user-defined signal and this shared memory developers can access to child process methods that returns a serializable variable.

SourceForge.net: PHPlet
PHPlet, is an Application Server written in PHP that serves PHP servet (<i>PHPlet</i>). PHPlet are similar to Java Servlet as they implements the init(), service(), destroy() methods. The lifecycle of PHPlet is the same of servlet.

Class: Daemon provides a base class that can create and manage daemon processes on Linux or other Unix-like systems.

This base class can fork the current process and store the child process identifier in a pid file. The forked process enters an infinite loop executing its job until it receives a termination signal.

The base class is not useful by itself. It needs to be derived to implement daemon job as a function of the sub-class. A daemon job subclass is provided as an example.

Article Fork PHP! brings another easy but cute solution. The fundamental thing is that you run a system command (php interpreter in fact) in background – example:

exec("/usr/local/bin/php /path/to/forked_script.php Value1 $ip > /dev/null &1");

Comments are closed.