在 PHP 中处理线程时如何防止重复的标头?

发布于 2024-08-11 03:18:53 字数 556 浏览 1 评论 0原文

我编写了一个 PHP 脚本,该脚本必须分叉并并行执行一些处理,然后主页在处理后返回数据。我目前正在使用 pcntl 函数来执行此操作。我需要在 2 到 10 个线程之间的任意位置运行才能并行工作,并且必须等到从所有线程获得结果后,脚本才会输出到浏览器。

不幸的是,我发现输出中出现了多个“Content-Type”标头:

Content-Type: text/plain

Content-Type: text/plain

Content-Type: text/plain
[... actual content ...]

似乎每个线程都有一个标头。在处理之前,我尝试通过 fclose(STDOUT) 关闭 STDOUT,并且我尝试通过输出缓冲来包围所有内容,但无济于事。我还没有发现其他人遇到这个问题,所以要么没有人尝试这样做,要么我正在做一些不寻常的事情。

我真的在寻找阻止这种情况发生的方法,或者关于完成此操作的替代方法的建议。

服务器设置为在 mod_suPHP 下使用 PHP 作为 CGI(如果这有什么区别的话)。

I've written a PHP script that has to fork and do some processing in parallel, and then the main page returns data after processing. I'm currently using the pcntl functions to do this. I need to run anywhere between 2 and 10 threads to work in parallel, and I have to wait until I get a result from all of the threads before the script outputs to the browser.

Unfortunately, I find that I have multiple "Content-Type" headers appearing in the output:

Content-Type: text/plain

Content-Type: text/plain

Content-Type: text/plain
[... actual content ...]

It seems to be one header per thread spawned. I've tried to close STDOUT via fclose(STDOUT) before processing, and I've tried surrounding everything by output buffering to no avail. I haven't yet found anyone else with this problem, so either nobody is trying to do it or I'm doing something that's off the beaten track.

I'm really looking for either ways to stop this happening, or suggestions on alternative ways to get this done.

The server is set up to use PHP as CGI under mod_suPHP, if that makes any difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2024-08-18 03:18:53

您尝试过会话变量吗?
您可以在每个脚本完成/成功时在每个脚本中设置一个会话变量,然后检查/等待它们在主进程中是否完成/成功。

Did you try session variables?
You could set a session variable in each of the scripts when they are complete/successful and then check/wait to see if they are complete/successful in your main process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文