PHP Classes

PHP Multithreading with Shared Memory: Run multiple functions at the same time

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-12-28 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 57 This week: 1All time: 10,479 This week: 75Up
Version License PHP version Categories
threads 1.0MIT/X Consortium ...5PHP 5, Unix, Language
Description 

Author

This package can run multiple functions at the same time.

The threads can take a function as a parameter and create a parallel process to call that function.

The package uses a shared memory class to exchange information between the primary and thread processes.

Picture of Cuthbert Martin Lwinga
  Performance   Level  
Name: Cuthbert Martin Lwinga <contact>
Classes: 3 packages by
Country: Canada Canada

Example

<?php
include_once("SharedMemoryHandler.php");
include_once(
"Threads.php");
use
NameSpaceThreads\Threads;

function
testbackground($param=1){
   
mt_srand(); // Seed the random number generator
   
$randomNumber = mt_rand(0, 4);
    echo
"\n $param will sleep for $randomNumber s\n";
   
sleep($randomNumber);
    echo
"\n $param woke up\n";
}

Threads::addTask("testbackground",[1]);
Threads::addTask("testbackground",[2]);
Threads::addTask("testbackground",[3]);
Threads::addTask("testbackground",[4]);
Threads::addTask("testbackground",[5]);
Threads::addTask("testbackground",[6]);
Threads::addTask("testbackground",[7]);
Threads::addTask("testbackground",[8]);
Threads::run($Threads=10);

echo
"\n\n all done \n\n";

?>


Details

? Multithreading with Shared Memory in PHP

_?? Efficient Threading & Memory Management for PHP Applications_

Build Status PHP Version

? Facing threading challenges in PHP? Let's tackle them together! My journey in developing a function for complex computations in PHP revealed several threading hurdles. These issues, such as deadlocks and inefficient I/O executions, often hampered performance. To combat this, I've innovated a PHP threading solution utilizing shared memory, aimed at speedy and effective data handling.

  • ? Implement multithreading in PHP
  • ? Utilize shared memory for top-notch data access
  • ?? Sidestep common deadlocks and boost performance
  • ? Craft a Robust & Scalable solution ?

? Solution Overview

Unlock threading prowess with the Threads class in the NameSpaceThreads namespace. This gem works alongside the SharedMemoryHandler for slick inter-process communication. Expect quicker data access and smooth management of concurrent tasks.

? Key Features

  • ? Efficient Process Management: Dynamically juggles multiple processes.
  • ? Shared Memory: Harnesses shared memory for swift data access. Dealing with loads of data? Modify `$default_size = 1000000;` to your needs, but watch out for resource Ops! ?
  • ? Dynamic Task Allocation: Smartly assigns tasks to threads, balancing the load. Imagine running 30 processes with the system intelligently distributing tasks to avoid overloading.
  • ?? Concurrency Control: I've adopted a savvy concurrency control strategy using `fork()`. This approach clones data, facilitating simultaneous thread execution sans inter-thread chatter. The result? Blazing-fast, streamlined processing with jaw-dropping outcomes.
  • ?? Resource Management: Keeps a tight rein on resources to prevent leaks. As well as proper handling of pid termination.

? Error Handling

  • ? Thread Collapse: Despite the fact that threads may occasionally drop out of execution due to various reasons such as buggy functions and resource constraints, the threads operate independently of one another and continue to perform their assigned tasks with no communication between them.

? Installation and Usage

Make sure PHP is up and running with pcntl and posix extensions on board.

git clone git@github.com:cuthbert-lwinga/php-threading.git


  Files folder image Files  
File Role Description
Accessible without login Plain text file Example_Usage.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation
Plain text file SharedMemoryHandler.php Class Class source
Accessible without login Plain text file ThreadedTaskDemo.php Example Example script
Plain text file Threads.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:57
This week:1
All time:10,479
This week:75Up