加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – jQuery:我可以在更长的$.ajax请求待处理时发送和接收$.a

发布时间:2020-12-13 13:35:27 所属栏目:PHP教程 来源:网络整理
导读:情况: 我正在使用$.ajax()POST向php脚本发送请求,该脚本将大约400,000-500,000行插入到db中.这一直需要3.5到4分钟. (在此期间,请求是PENDING). 问题: 我需要一些方法来显示页面上的进度. (比如一个 %).我尝试在setInterval中使用$.ajax(),每隔5秒左右检查
情况:
我正在使用$.ajax()POST向php脚本发送请求,该脚本将大约400,000-500,000行插入到db中.这一直需要3.5到4分钟. (在此期间,请求是PENDING).

问题:
我需要一些方法来显示页面上的进度. (比如一个 %).我尝试在setInterval中使用$.ajax(),每隔5秒左右检查一次,但它们似乎会在第一个(更长的)$.ajax()完成时生成.

题:
默认情况下不是$.ajax()async吗?这不应该意味着可以在任何时间以任何顺序发送请求,并且应该以任何顺序随时收到响应吗?这甚至与异步有什么关系吗?有没有办法定期从一个请求发回“半响应”?或者,当有待处理的请求/响应时,我是否无法发送和接收请求/响应? (见下面的精彩图纸)

提前致谢!!!

multiple requests http://kshaneb.com/reqres.png

您长时间运行的ajax调用可能会在服务器上打开一个会话,因此会话文件锁定会阻止所有下一个请求.

Problem:
PHP writes its session data to a file by default. When a request is made to a PHP script that starts the session (session_start()),this session file is locked. What this means is that if your web page makes numerous requests to PHP scripts,for instance,for loading content via Ajax,each request could be locking the session and preventing the other requests from completing.
The other requests will hang on session_start() until the session file is unlocked. This is especially bad if one of your Ajax requests is relatively long-running.

Solution:
The session file remains locked until the script completes or the session is manually closed. To prevent multiple PHP requests (that need $_SESSION data) from blocking,you can start the session and then close the session. This will unlock the session file and allow the remaining requests to continue running,even before the initial request has completed.

更多信息:
http://konrness.com/php5/how-to-prevent-blocking-php-requests/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读