【问题标题】:Alowing execution of long script after output without blocking the user允许在输出后执行长脚本而不阻塞用户
【发布时间】:2013-06-26 10:07:36
【问题描述】:

我今天在我们正在开发和重构的应用程序中遇到了一些问题。我们目前有一个巨大的“用户”对象,其中包含有关登录用户及其权限的大量信息。一切都在用户登录后立即完成,并在会话中序列化。序列化现在开始需要一些时间,而且我发现有很多 __destruct 脚本等。

因此,考虑到这一点,我想知道是否可以让 PHP 在页面提供给客户端后执行所有这些“关闭”步骤,而不保持 HTTP 连接打开,因为它将显示为页面仍在加载并阻止某些客户端代码执行。

知道我在 Zend 上运行可能会有所帮助... 我试图显式关闭输出缓冲区,而不是等待 php 在脚本结束时执行此操作,但同样的问题仍然存在。我还关闭了会话以确保这不是它的错,但它会阻止用户对象将自己保存在会话中的 __destruct 上。

感谢您的建议和/或解释。

PS.:我知道整个事情必须重做,因为这不是一个很好的方法,但我们现在没有时间。

【问题讨论】:

    标签: php stdout


    【解决方案1】:

    也许这会对你有所帮助。 http://php.net/manual/en/features.connection-handling.php

    <?php
    // redirecting...
    ignore_user_abort(true);
    header("Location: ".$redirectUrl, true);
    header("Connection: close", true);
    header("Content-Length: 0", true);
    ob_end_flush();
    flush();
    fastcgi_finish_request(); // important when using php-fpm!
    
    sleep (5); // User won't feel this sleep because he'll already be away
    

    【讨论】:

    • 甜,这似乎可以做到...只是没有标题我认为它会完成这项工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    相关资源
    最近更新 更多