Possible Duplicate:
php execute a background process
how can I achieve a task that should be done in thread in php
I have this PHP script that takes a little long to process. However I don’t want the viewer to wait for the script to finish. I want him to be able to continue browsing and/or close the browser. Yet I want the script to continue to work… How can I do this? Is there way to create some sort of thread that will allow such a thing?
p.s. I really don’t want to re-write the script in another language and execute it via os.
Avoid the idea of running paralell tasks (threads or processes) in PHP. It has some modules for this purpose, but they are not available on every platform and are not part of the PHP core, so the chances for you to be able to use them are very low.
You have two options here:
Split the long running task into several (cheaper) sub-tasks. Put them on a queue and make each request execute some of these sub-tasks (but don’t delay the output to the client too much).
If you can’t split this big task, just setup a cron job for it. This way users are unnafected by the delay as the long running task is execute by the system.
Another thing to note is that PHP script have (an overridable) time limit that halts execution after a given amount of seconds. Tasks that run via cron don’t suffer this limit.