my code stock.com

mburknoe

Mikes PHP MultiThreading Engine
by mburknoe

Let me start by saying that this isn't true MultiThreading. It's MultiProcessing, which in PHP is about as close as we can get while still keeping is simple and where everything works. You will find that it's quite powerful.

The basic concept consists of three players:
1. The Controller
This object is the master function that is responsible for determining what threads need to be created and creating them
2. The Thread
This object is the function that takes time and is run for many objects - It is created repeadidly by the controller
3. The Control-DB
This is how the controller and the threads talk to one another, and how we keep a good idea on what's happening during the process and what happened once the process is complete.

Snippet options

Download: Download snippet as mikes-php-multithreading-engine.php.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

//1. THE CONTROLLER
//controller.php

foreach($objectsToThread as $current{

	// $current['Filename'] = $argv[1];
	// $current['Hostname'] = $argv[2];

	$log->LogInfo("<<< Creating thread event for $current[Filename] $current[Hostname] >>>");
	//ADD "> /dev/null 2>/dev/null &" for fire and forget :)
	shell_exec("php -f thread.mt.php $current[Filename] $current[Hostname] > /dev/null 2>/dev/null &");
};

//2. THE THREAD
//thread.php

//Pull in the info from the controller

$current = array();
$current['Filename'] 	= $argv[1];
$current['Hostname'] 	= $argv[2];

//Do something with it

//3. THE CONTROL DB
//Need to write this

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.