14

I made a long script in PHP as such:

ignore_user_abort(true);
set_time_limit(0);

It runs perfectly in the background even if I close the page. My problem is that I can't open other PHP files until this script finishes running in the background. How can I solve this problem?

7
  • 1
    Does this script plus the pages that get locked use sessions? Commented Nov 30, 2012 at 18:48
  • Until the long script is running, or until it is finished running? Commented Nov 30, 2012 at 18:51
  • @SalmanA Yes my files are using sessions, is that problem? Commented Nov 30, 2012 at 19:00
  • @cegfault Sorry for my english, so I cant use other php files while the long script is running. I hope you understand what I try to say. Commented Nov 30, 2012 at 19:00
  • Please post the code that is using the sessions; that is most likely where the problem is Commented Nov 30, 2012 at 19:03

2 Answers 2

31

When a PHP script uses sessions, PHP locks the session file until the script completes. A page request that tries to use a locked session is blocked until the session file is released. PHP does this so that sessions remains in a consistent state. Quote from PHP bug #31464:

[2005-01-10 08:13 UTC] derick at php dot net

This is indeed not a bug at all, the session extension needs to lock the session file so that concurrent updates can not corrupt the file. This means that all scripts using the same session file needs to be serialized. To improve performance you can use http://php.net/session_write_close as soon as you are done reading/setting session variables, which will remove the lock of the file.

The simplest workaround as described above and here as well is:

  • call session_start()
  • read/write any session variables
  • call session_write_close()
  • do lengthy processing
Sign up to request clarification or add additional context in comments.

Comments

11

As mentioned in comments, Sessions are the problem - this is because the session file is locked.

Use session_write_close() in your long-running script to unlock the session file, but note that you cannot use $_SESSION variables in that particular script afterwards.

1 Comment

Thank you for your answer Kolink, I offered to accept SalmanA's answer if he wants to write one because he was the first. Other cases I'll accept your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.