1

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?

tshepang
  • 10,772
  • 21
  • 84
  • 127
i need help
  • 2,030
  • 10
  • 50
  • 69

2 Answers2

2

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: PHP- Need a cron for back site processing on user signup... (or fork process)

Community
  • 1
  • 1
deceze
  • 471,072
  • 76
  • 664
  • 811
0

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
  • 2,030
  • 10
  • 50
  • 69
  • 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 Aug 24 '09 at 04:26