Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...