├── composer.json ├── samples └── Example.php ├── Threading ├── Task │ ├── Base.php │ └── Sample.php └── Multiple.php └── README.md /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alfallouji/php_multithread", 3 | "description": "iPHP library to handle multi-threading", 4 | "authors": [ 5 | { 6 | "name": "Bashar Al-Fallouji", 7 | "email": "alfallouji@gmail.com", 8 | "homepage": "http://bashar.alfallouji.com", 9 | "role": "Developer" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.3.0" 14 | }, 15 | "autoload": { 16 | "classmap": [ "Threading/"] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /samples/Example.php: -------------------------------------------------------------------------------- 1 | start($exampleTask); 34 | } 35 | -------------------------------------------------------------------------------- /Threading/Task/Base.php: -------------------------------------------------------------------------------- 1 | maxThreads = $maxThreads; 52 | } 53 | 54 | /** 55 | * Start the task manager 56 | * 57 | * @param AbstractTask $task Task to start 58 | * 59 | * @return void 60 | */ 61 | public function start(AbstractTask $task) 62 | { 63 | $pid = pcntl_fork(); 64 | if ($pid == -1) 65 | { 66 | throw new \Exception('[Pid:' . getmypid() . '] Could not fork process'); 67 | } 68 | // Parent thread 69 | elseif ($pid) 70 | { 71 | $this->_activeThreads[$pid] = true; 72 | 73 | // Reached maximum number of threads allowed 74 | if($this->maxThreads == count($this->_activeThreads)) 75 | { 76 | // Parent Process : Checking all children have ended (to avoid zombie / defunct threads) 77 | while(!empty($this->_activeThreads)) 78 | { 79 | $endedPid = pcntl_wait($status); 80 | if(-1 == $endedPid) 81 | { 82 | $this->_activeThreads = array(); 83 | } 84 | unset($this->_activeThreads[$endedPid]); 85 | } 86 | } 87 | } 88 | // Child thread 89 | else 90 | { 91 | $task->initialize(); 92 | 93 | // On success 94 | if ($task->process()) 95 | { 96 | $task->onSuccess(); 97 | } 98 | else 99 | { 100 | $task->onFailure(); 101 | } 102 | 103 | posix_kill(getmypid(), 9); 104 | } 105 | pcntl_wait($status, WNOHANG); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Authors & contact 3 | 4 | 5 | Al-Fallouji Bashar 6 | - bashar@alfallouji.com 7 | 8 | 9 | ## Documentation and download 10 | 11 | 12 | Latest version is available on github at : 13 | - http://github.com/alfallouji/PHP-Multithread/ 14 | 15 | 16 | ## License 17 | 18 | 19 | This Code is released under the GNU LGPL 20 | 21 | Please do not change the header of the file(s). 22 | 23 | This library is free software; you can redistribute it and/or modify it 24 | under the terms of the GNU Lesser General Public License as published 25 | by the Free Software Foundation; either version 2 of the License, or 26 | (at your option) any later version. 27 | 28 | This library is distributed in the hope that it will be useful, but 29 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 30 | or FITNESS FOR A PARTICULAR PURPOSE. 31 | 32 | See the GNU Lesser General Public License for more details. 33 | 34 | 35 | ## Description 36 | 37 | 38 | This library provides a lightweight / simple PHP Oriented Object classe to handle multi-threading. Check the sample folder for examples on how to use it. This library requires to have the pcntl (http://php.net/pcntl) extension installed and it will only work with unix distributions. 39 | 40 | ## Setup 41 | 42 | You can use composer to use this library. 43 | 44 | ``` 45 | { 46 | "require": { 47 | "alfallouji/php_multithread": "*" 48 | } 49 | } 50 | ``` 51 | 52 | 53 | ## Usage 54 | 55 | This client does not rely or depend on any framework and it should be fairly easy to integrate with your own code. You can use composer or your own custom autoloader. 56 | 57 | The sample folder contains example on how to use this library. 58 | 59 | ## Example 60 | 61 | ### Multi-threading a simple task 62 | ``` 63 | require(__DIR__ . '/../vendor/autoload.php'); 64 | 65 | $maxThreads = 5; 66 | echo 'Example of the multi-thread manager with ' . $maxThreads . ' threads' . PHP_EOL . PHP_EOL; 67 | $params = array(); 68 | $exampleTask = new Threading\Task\Example($params); 69 | $multithreadManager = new Threading\Multiple(); 70 | 71 | $cpt = 0; 72 | while (++$cpt <= 30) 73 | { 74 | $multithreadManager->start($exampleTask); 75 | } 76 | ``` 77 | 78 | Will provide following output : 79 | ``` 80 | Example of the multi-thread manager with 5 threads 81 | 82 | [Pid:23447] Task executed at 2015-04-03 14:49:18 83 | [Pid:23448] Task executed at 2015-04-03 14:49:18 84 | [Pid:23449] Task executed at 2015-04-03 14:49:18 85 | [Pid:23450] Task executed at 2015-04-03 14:49:18 86 | [Pid:23451] Task executed at 2015-04-03 14:49:18 87 | [Pid:23452] Task executed at 2015-04-03 14:49:19 88 | [Pid:23454] Task executed at 2015-04-03 14:49:19 89 | [Pid:23453] Task executed at 2015-04-03 14:49:19 90 | [Pid:23455] Task executed at 2015-04-03 14:49:19 91 | [Pid:23456] Task executed at 2015-04-03 14:49:19 92 | [Pid:23457] Task executed at 2015-04-03 14:49:20 93 | [Pid:23458] Task executed at 2015-04-03 14:49:20 94 | [Pid:23459] Task executed at 2015-04-03 14:49:20 95 | [Pid:23460] Task executed at 2015-04-03 14:49:20 96 | [Pid:23461] Task executed at 2015-04-03 14:49:20 97 | [Pid:23463] Task executed at 2015-04-03 14:49:21 98 | [Pid:23462] Task executed at 2015-04-03 14:49:21 99 | [Pid:23464] Task executed at 2015-04-03 14:49:21 100 | [Pid:23465] Task executed at 2015-04-03 14:49:21 101 | [Pid:23466] Task executed at 2015-04-03 14:49:21 102 | [Pid:23467] Task executed at 2015-04-03 14:49:22 103 | [Pid:23468] Task executed at 2015-04-03 14:49:22 104 | [Pid:23470] Task executed at 2015-04-03 14:49:22 105 | [Pid:23469] Task executed at 2015-04-03 14:49:22 106 | [Pid:23471] Task executed at 2015-04-03 14:49:22 107 | [Pid:23472] Task executed at 2015-04-03 14:49:23 108 | [Pid:23473] Task executed at 2015-04-03 14:49:23 109 | [Pid:23474] Task executed at 2015-04-03 14:49:23 110 | [Pid:23475] Task executed at 2015-04-03 14:49:23 111 | [Pid:23476] Task executed at 2015-04-03 14:49:23 112 | ``` 113 | --------------------------------------------------------------------------------