Created
September 14, 2017 13:34
-
-
Save kuntalchandra/72257ceef92921f19b93fab47fa31a51 to your computer and use it in GitHub Desktop.
Forking multiple processes in PHP using pcntl_fork()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| include("Lib.php"); | |
| class ImageResizerMultiProcess | |
| { | |
| private $sizes = []; | |
| public function setSizes($sizes) | |
| { | |
| $this->sizes = $sizes; | |
| return $this; | |
| } | |
| public function getSizes() | |
| { | |
| return $this->sizes; | |
| } | |
| public function executeMultiProcess() | |
| { | |
| $childPids = array(); | |
| $sizes = $this->getSizes(); | |
| foreach ($sizes as $size) { | |
| $pid = pcntl_fork(); | |
| if ($pid == -1) { //fork failed. May be extreme OOM condition | |
| die('pcntl_fork failed'); | |
| } elseif ($pid) { //parent process | |
| $childPids[] = $pid; | |
| } else { //child process | |
| $status = Lib::resizeImage($size[0], $size[1]); | |
| exit(); | |
| } | |
| } | |
| while (!empty($childPids)) { //wait for all children to complete | |
| foreach ($childPids as $key => $pid) { | |
| $status = null; | |
| $res = pcntl_waitpid($pid, $status, WNOHANG); | |
| if ($res == -1 || $res > 0) { //if the process has already exited | |
| unset($childPids[$key]); | |
| } | |
| } | |
| //here sleep() should be used, if the script is in production and doing some heavy process | |
| } | |
| } | |
| } | |
| $requiredSizes = [[50, 50], [100, 100], [150, 150], [200, 200], [250, 250], [300, 300], [350, 350], [400, 400], [450, 450], [500, 500], [1000, 1000]]; | |
| $obj = new ImageResizerMultiProcess(); | |
| $obj->setSizes($requiredSizes); | |
| $obj->executeMultiProcess(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| include("Lib.php"); | |
| class ImageResizerSingleProcess | |
| { | |
| private $sizes = []; | |
| public function setSizes($sizes) | |
| { | |
| $this->sizes = $sizes; | |
| return $this; | |
| } | |
| public function getSizes() | |
| { | |
| return $this->sizes; | |
| } | |
| public function execute() | |
| { | |
| $sizes = $this->getSizes(); | |
| foreach ($sizes as $size) { | |
| $status = Lib::resizeImage($size[0], $size[1]); | |
| } | |
| } | |
| } | |
| $requiredSizes = [[50, 50], [100, 100], [150, 150], [200, 200], [250, 250], [300, 300], [350, 350], [400, 400], [450, 450], [500, 500], [1000, 1000]]; | |
| $obj = new ImageResizerSingleProcess(); | |
| $obj->setSizes($requiredSizes); | |
| $obj->execute(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Lib | |
| { | |
| const UPLOADED_IMAGE_DIR = '/var/tmp/image/'; | |
| const RESIZED_IMAGE_DIR = '/var/tmp/resized/'; | |
| public static function resizeImage($height, $width) | |
| { | |
| $image = self::UPLOADED_IMAGE_DIR . 'test.JPG'; | |
| $newImage = self::RESIZED_IMAGE_DIR . 'test-' . $width . '-' . $height . '.JPG'; | |
| list($originalwidth, $originalheight) = getimagesize($image); | |
| $tmpImage = imagecreatetruecolor($width, $height); | |
| $copiedImage = imagecreatefromjpeg($image); | |
| imagecopyresampled($tmpImage, $copiedImage, 0, 0, 0, 0, $width, $height, $originalwidth, $originalheight); | |
| return imagejpeg($tmpImage, $newImage, 100); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment