开发者

Need to execute a code in background

I have done a code to receive images from iphone to PHP Server and I need to resize these image and move to 4 folders.

Only then the json respose is giving to iphone. But it takes m开发者_运维问答uch time.

Requirement:

  1. i want to move a file to the folder "folder1" then want to give the json response.

the resizing process should do from this "folder1" after giving json response.

How to run this resizing process in background.

Here is my code:

http://pastebin.com/qAcT1yi9


You could always send your php script to run in the background with a Linux command.

Example:

// using backticks to execute the Linux command but there are 
// other alternatives 
$cmd = `php runScriptInBackground.php &`;
echo $cmd;


First send/upload the images and send a response back, without doing the resize operation.

Then, if the upload was successful, let the browser issue another request and do the resizing. When this succeeds, send the message ‘resizing successful’ back.


A common solution to this problem is implementing a loading/processing message on hitting a specific event. Then - still being displayed - the action will continue to load on the background and the result page will finally be displayed when done.

Although the user must wait, I prefer this above display a result message when the actual result is not known. Unfortunately I'm not sure how this is done on iphone development.


if your building in objective c then you may just resize make a copy and resize it there and send the resized image to your php you could then display a spinner and json result back to the user and also if the is an error the user will still have the resized image to try again with... Also another thought I had was was to use push notification. I don't know what that code would look like but it's something to consider


you need some async javascript or an iframe in your page posting the image to your server and providing feedback to the user.

This means that the 'main' page would not change, but some visual information can be provided to the user.

You can display an animated gif loader or use JS setInterval to give the user the feeling that things are moving forward why waiting for the server to respond.

If the processing is split in more 1 parts, after each step the server could respond with an HTML page and a redirect: this would even work in an IFRAME without JS. Each 'page' would perform one more step. But if the user closes the browser before all is done you would end with an unfinished task.

A DB, real background processing, and client side JS polling are a more robust alternative.

A full answer would be quite long and require way more details on your settings (apache CGI PHP? or mod_php? are you using an MVC model or framework, or are you writing a page-oriented website?).

If i had to write a full answer I would forget PHP and use Python and celery http://celeryproject.org/ ;-)

PS.

I just found out that a few related questions already existed:

  • PHP Background Processes
  • Asynchronous shell exec in PHP


You can do it realy in two times, first send de files and save on first server, after when the user request that you generate the necesary parts.

You will pass the costs from the file sender to the first request from that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜