-2

I decided to use PHP for my last project, coming from the Java world.

I searched online but it seems that all answers are quite complex to implement and I thought there would be an easier way.

The problem is straightforward how to do parallel processing/tasking in PHP? The diagram below illustrated what I want to achieve, I am creating a simple page something like a product where users can add product info and put some images. Images are stored in Blob storage like s3, before images are stored they are resized on the served/also in the front end but that is not the point here.

In Java I would use multithreading to resize each image and save it to S3 it will definitely speed up the process as one image will not wait for the previous one to be resized and will not wait for the previous one to be stored in S3. How can I achieve this in PHP, assuming that my machine has 1,2,3 ... n CPUs?

enter image description here

Adelin
  • 18,144
  • 26
  • 115
  • 175
  • 1
    Does this answer your question? [How can one use multi threading in PHP applications](https://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications) – kmoser Nov 13 '22 at 19:04
  • Pthreads is DEAD. Response is out dated. Fresh response would be great. – JoelCrypto Nov 13 '22 at 20:09
  • https://www.php.net/manual/en/function.pcntl-fork.php is very eay to use. – Markus Zeller Nov 13 '22 at 20:36
  • You can initiate shell_exec() to initiate processing and upload images to s3 passing the image file parameter within a foreach loop. This will initiate multiple instance of php. shell_exec(path-to-php/php path-to-image-processing-script/script.php >> path-to-error-log/err.log 2>&1 & echo $!); Useful to run multiple instance of php at same time. – Zubair Ahmd Nov 14 '22 at 01:15

1 Answers1

0

Create file that shall receive the initial request as following; then create script.php file which shall receive the image data and process it.

<?php
//code to receive request and create db record
//after that

foreach(images as image){
   shell_exec(path-to-php/php path-to-image-processing-script/script.php?key=image >> path-to-error-log/err.log 2>&1 & echo $!);

}
?>
Zubair Ahmd
  • 208
  • 1
  • 9