2

我目前有一个 AJAX 请求发送到 sendMail.php,它立即关闭连接(使用标头Connection: Close)并继续处理大约 30 秒。

但是我现在遇到的问题是,当同一个客户端尝试从该服务器加载任何 PHP 页面时,它必须等到 sendMail.php 完成处理。

有没有办法解决?

我正在阅读其他一些可能与会话相关的 SO 问题,但我没有使用任何会话,我什至尝试在 sendMail.php 脚本的开头调用session_write_close() 。

示例代码(这是hacky并且完成了,但它有效):

   //File: SendMail.php
   //error_reporting(E_ALL);
    error_reporting(0);
    session_write_close();
    set_time_limit(0); 
    ignore_user_abort(1);
    ignore_user_abort(true);
    ini_set('ignore_user_abort','1');
    apache_setenv('no-gzip', 1);
    apache_setenv('KeepAlive',0);
    ini_set('zlib.output_compression', 0);
    ini_set('output_buffering', 0);

    $size = ob_get_length();

    // send headers to tell the browser to close the connection
    header("Content-Length: $size");
    header('Connection: Close');
        // flush all output
    ignore_user_abort(true);
    ob_end_flush();
    ob_flush();
    flush();
    sleep(30);//The real code has more stuff, but just for example lets just say it sleeps for 30 seconds

其余参考资料是通过 GET 进行的正常导航。

4

1 回答 1

0

几乎听起来您的 AJAX 调用正在调用一个脚本,该脚本正在消耗服务器上的可用资源(无论是内存/CPU/磁盘 I/O/等),并且阻止任何新脚本能够运行。仔细检查以确保您分配了足够的资源来完成工作。

附带说明一下,通常您不允许您的日常用户生成在服务器上运行那么长时间的进程,这仅仅是因为您为他们提供了使您的系统超载并为他们提供 DDOS 攻击的简单机会的机会。如果有可能,我会考虑 Ibere 将其移至 cron 的建议。

于 2012-10-24T19:41:29.910 回答