0

So I have a php file that runs sets of sql queries for a list of ids. This file may take some time to run.

While that is running, I want to have a progress bar.

I have it so that the running file updates a session variable indicating the percentage of completion after each set of queries. Then in my front end, I'm doing an ajax call to another file that reads the session variable value and returns it. I then update the width of the progressbar fill with the returned percentage.

The problem: The progress bar isn't updated until the all the queries are completely done. So I see my progress bar at 0% for some seconds then it suddenly jumps to 100%. It seems that the session is locked until the script completes. I tried using session_write_close() but cannot get it to work.

Thanks.

Rickkwa
  • 2,197
  • 4
  • 23
  • 34
  • 1
    Session files in PHP are locked the time the session runs. So from `session_start()` to `session_write_close()`. See as well [PHP & Sessions: Is there any way to disable PHP session locking?](http://stackoverflow.com/q/3371474/367456), [PHP session_start() causing HTTP requests to hang](http://stackoverflow.com/q/2259112/367456) and the many other ones regarding the topic. As it's sessions you need to fiddle a it around with it. – hakre Aug 20 '13 at 15:34

2 Answers2

1

I had a similar problem, and the session solution did not work. My solution was to wrote the current status into a temporary .txt file, and then read that file with ajax call.

Danijel
  • 12,408
  • 5
  • 38
  • 54
1

The problem with doing it this way is that session variables are not stored at runtime. They will wait until the script completed before storing the variable in the session. One solution would be to store the progress temporarily in a database or a file and accessing the data periodically with AJAX.

Another solution would be to use server side javascript like Googles V8 Engine (Node.js) and fire events based on your progress. You're page would then update based on the javascript events being fired.

Matthew R.
  • 4,332
  • 1
  • 24
  • 39