Search code examples
phpflushcontinue

php continue execution after leaving the page


I have a simple script that counts from 1 to 5000 with a for loop. It flushes output in real time to browser and shows a progress bar with %.

What I have: If I leave the page, the process interrupts. If I come back, it starts from 0.

What I want to achieve: If I leave the page, the process continues and, If I come back , it shows the right percentage.

Example: I run the process, it counts till 54, I leave the page for 10 seconds, when I come back it shows me 140 and continues to flush.

Is it possible?


Solution

  • I would suggest you to use server workers - scripts which are intended to run independently from webserver context.

    The most common way of doing it - usage of message queues (RabbitMQ, Qless, etc). Event should be initiated by the script in web context, but the actual task should be executed by queue listener in a different context.