长时间运行的后台PHP脚本会阻止其他PHP页面,直到完成为止 [英] Long running background PHP script blocks other PHP pages until it is finished
问题描述
我在PHP中编写了一个长脚本,如下所示:
ignore_user_abort(true);
set_time_limit(0);
即使我关闭页面,它也可以在后台完美运行.我的问题是,直到此脚本在后台运行完毕,我才能打开其他PHP文件.我该如何解决这个问题?
当PHP脚本使用会话时,PHP会锁定会话文件,直到脚本完成.尝试使用锁定的会话的页面请求将被阻止,直到释放会话文件为止. PHP这样做是为了使会话保持一致状态. 引用PHP错误#31464 :
[2005-01-10 08:13 UTC] php点网的德里克
这确实根本不是错误,会话扩展需要锁定 会话文件,以便并发更新不会损坏该文件. 这意味着使用同一会话文件的所有脚本都需要 序列化.为了提高性能,您可以使用 完成操作后, http://php.net/session_write_close 读取/设置会话变量,这将删除 文件.
上述和此处也是最简单的解决方法是:>
- 致电
session_start()
- 读取/写入任何会话变量
- 致电
session_write_close()
- 进行冗长的处理
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?
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
这篇关于长时间运行的后台PHP脚本会阻止其他PHP页面,直到完成为止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!