views:

394

answers:

3

Hi there,

This is the case. At test.php, I have a function dotask(a,b,c,d);

This function need to do task that need 2-4 minutes to complete. Task including insert new record into db, curl call to other url to do task and etc.

However, I want test.php to: Just make sure dotask(a,b,c,d) is called, no need to wait until task completed then only return and continue with the remaining code at bottom.

Reason: test.php is a thank you page. I can't expect the user to wait few minutes for the task to be completed. Because I need to show thank you messages etc at that page to user.

What can I do?

+1  A: 

You can't fork a process in PHP without a lot of hackery. I'd recommend using a queue and worker pattern instead. See this answer: http://stackoverflow.com/questions/984577/php-need-a-cron-for-back-site-processing-on-user-signup-or-fork-process/984592#984592

deceze
A: 

here is something else you can look at, a few different solutions here.

http://stackoverflow.com/questions/1019867/php-shell-execute-without-waiting-for-a-return

John Boker
A: 

I seen a few solutions here that require you to pass in the page that you want to run, eg: BackgroundProcess::fork('process_user.php');

But my case is at test.php, I have dotask(a,b,c,d) I need to pass in parameters from this site to continue the work.

So should I just pass in this few parameter into a new dbtable call pendingprocess, then at process_user.php , I read from database and continue the task instead?

I can't embed parameter into taskpage.php right...

Another solution I can think of is at thankyou.php I do a body onload ajax call by passing in parameter to process_user.php to perform task. Anyone can advice whether this is a good way? Will the process stop executing when user click STOP at browser? What if they go to refresh the browser.

i need help
Yes, queue tasks in a database with whatever kind of extra information you need. You could also write to a text file. Do whatever seems most appropriate to store the information for another process to pick up.
deceze