-2

I have a PHP program which works like this

  1. User uploads an image
  2. Image is processed
  3. Other time intensive functions are called
  4. Upload confirmation is sent to user.

The problem is that items 2 and 3 take up a lot of time - how can I defer these processes to be run asynchronously?

I would like to start the running of the processes directly after the image is uploaded, but still get on with doing other things and not wait.

I know about cron, but I don't want to have to schedule the task - I just don't want to have to wait for it to complete.

What are my options?

EDIT

Just to be clear this project is server side only. There is no javascript option.

Mazatec
  • 11,481
  • 23
  • 72
  • 108

3 Answers3

2

First I suggest you to reorder your tasks:

  1. User uploads an image
  2. Upload confirmation is sent to user.

  3. Image is processed

  4. Other time intensive functions are called

Then you have following options, how to deffer parts 3&4:

Option 1: Send confirmation to user on step 2, use ignore_user_abort() and continue with handling image (non optimal, as user will still see "page loading" message.

Option 2: IBP-style. Do steps 1-2, then add processing job to some sort of database. At random time show image to the users.. Image is actually link to the php script that do ignore_user_abort() and process your queue of items.

Option 3: Same as option 2, but don't show image. Instead, you will have to do request to own server with cURL, or similar. Idea is that you start request, and don't wait for it to end, just stop main script execution.

Option 4: start a cron script that starts every 5-15 minutes and do needed tasks. It can also live on background 24h, and sleep for 20 seconds between checking for tasks. This option might be best in case you have relatively busy website.

Option 5: try to do something like exec() or shell_exec() to ruin processing script on background, after you added task to the queue.

Examples of how to do options 2 & 3: Asynchronous PHP calls? How do I make an asynchronous GET request in PHP?

Community
  • 1
  • 1
Oleg Liski
  • 563
  • 4
  • 15
  • What if 3 and 4 fails ? Confirmation is already sent. – Abhishek Saha Oct 24 '12 at 10:09
  • step 2 says that image is uploaded, not processed, so I don't see a problem with steps 3&4 failed :) In addition to that.. If there is queue implemented, you can easilly reprocess image that is failed in next script instance. (you can do this with options 2-5, not with option 1) – Oleg Liski Oct 24 '12 at 10:21
  • Optoin 4 is the most reliable in my experience. +1. Thanks. – dotancohen Oct 24 '12 at 18:35
2

Split, if needed, your code in two, so the time consuming logic is in a separate script, e.g. timeConsumer.php. Then you can create a child process using pcntl-fork() and execute your timeConsumer.php in it.

Alternatively you can use exec() or any of the other possible ways listed in the documentation. On Unix/Linux systems you can then add an & at the end of your command string, so the program would be executed in the background.

You can pass the name of uploaded image to your timeConsumer.php as a command line parameter, which is then accessible through the global variable $argv. (which is an array)

Havelock
  • 6,913
  • 4
  • 34
  • 42
-2

You need to use ajax/jquery for this. After uploading the image call this. See blow.

$.ajax({
     url: 'process.php',
     type: 'post',
     success: function(data) {

          //this is triggered when 2 and 3 is done.

     }


});
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29