PHP 站点正在等待 ajax 调用完成 [英] PHP site is waiting on ajax calls to complete

查看:59
本文介绍了PHP 站点正在等待 ajax 调用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 PHP 站点,可以进行一些大量的 ajax 调用.它们大约需要 8-10 秒,并通过 cURL 请求与许多 3rd 方 API 通信.

I've got this PHP site that makes some hefty ajax calls. They take about 8-10 seconds and communicate with a lot of 3rd party APIs via cURL requests.

当这些 ajax 调用正在进行时,站点的其余部分没有响应.不仅仅是这个页面,还有另一个页面上的另一个浏览器标签.

When these ajax calls are going on, the rest of the site is unresponsive. Not just this page, but another browser tab on another page all together.

我应该检查什么以确保 PHP 能够分叉多个进程,从而使站点不会挂起?

what should I check to make sure PHP is able to fork multiple processes so the site doesn't hang?

PHP 作为 PHP-fpm 运行

PHP is run as PHP-fpm

location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php5-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
    fastcgi_buffers 256 64k;
    fastcgi_buffer_size 2400k;
    fastcgi_read_timeout 6000;
    uwsgi_read_timeout 6000;
    include fastcgi_params;
}

而且 nginx 有足够的工作进程

And nginx has enough worker procs

user www-data;
worker_processes 16;
pid /var/run/nginx.pid;

events {
   worker_connections 8000;
   multi_accept on;
}

另外,我已经用 sleep() 和长 for 循环进行了测试.这两个都很好.似乎只是对外部网站的 cURL 调用导致了问题.

Also, I've tested with sleep() and a long for loop. Both of those are fine. It seems just the cURL calls to external sites that cause the issue.

不确定缺少什么.

谢谢!

推荐答案

听起来你需要调用 session_write_close

当脚本写入会话时,PHP 会锁定会话文件,因此每个页面都必须等待该页面完成执行才能打开会话(除非您在写入后调用上述函数).

PHP locks the session file when a script writes to the session, so each page has to wait for that one to finish executing before it can open the session (unless you call the above function after the write).

例如,您有一个长时间运行的 Ajax 进程,它在开始时写入会话.如果您不调用 session_write_close 来告诉 PHP 您的脚本已完成写入会话,它将锁定所有其他脚本的会话,直到执行完成.

Say for example you have a long running Ajax process that writes to the session at the beginning. If you don't call session_write_close to tell PHP your script is done writing to the session, it will lock up the session for all other scripts until execution is complete.

这篇关于PHP 站点正在等待 ajax 调用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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