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

前端之家收集整理的这篇文章主要介绍了php – jQuery:我可以在更长的$.ajax请求待处理时发送和接收$.ajax响应吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
情况:
我正在使用$.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/

猜你在找的PHP相关文章