长时间运行的后台 PHP 脚本会阻止其他 PHP 页面,直到它完成 [英] Long running background PHP script blocks other PHP pages until it is finished

查看:18
本文介绍了长时间运行的后台 PHP 脚本会阻止其他 PHP 页面,直到它完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 PHP 编写了一个很长的脚本:

ignore_user_abort(true);设置时间限制(0);

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

解决方案

当 PHP 脚本使用会话时,PHP 会锁定会话文件,直到脚本完成.尝试使用锁定会话的页面请求将被阻止,直到会话文件被释放.PHP 这样做是为了使会话保持一致的状态.引用自 PHP 错误 #31464:

<块引用>

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

这确实不是bug,会话扩展需要锁定会话文件,以便并发更新不会损坏文件.这意味着使用相同会话文件的所有脚本都需要连载.为了提高性能,您可以使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆