Long running background PHP script blocks other PHP pages until it is finished

前端 未结 2 1232
误落风尘
误落风尘 2020-12-03 14:59

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 pa

相关标签:
2条回答
  • 2020-12-03 15:29

    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
    0 讨论(0)
  • 2020-12-03 15:30

    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.

    0 讨论(0)
提交回复
热议问题