编程语言
首页 > 编程语言> > 长时间运行的后台PHP脚本会阻止其他PHP页面,直到完成为止

长时间运行的后台PHP脚本会阻止其他PHP页面,直到完成为止

作者:互联网

我在PHP中制作了一个长脚本:

ignore_user_abort(true);
set_time_limit(0);

即使我关闭页面,它也能在后台完美运行.我的问题是,在此脚本在后台运行之前,我无法打开其他PHP文件.我怎么解决这个问题?

解决方法:

当PHP脚本使用会话时,PHP会锁定会话文件,直到脚本完成.尝试使用锁定会话的页面请求将被阻止,直到会话文件被释放. PHP执行此操作以使会话保持一致状态. 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
07001 as soon as you are done
reading/setting session variables, which will remove the lock of the
file.

如上所述和here as well最简单的解决方法是:

>调用session_start()
>读/写任何会话变量
>调用session_write_close()
>做冗长的处理

标签:php,session,background-process
来源: https://codeday.me/bug/20190926/1818948.html