在 PHP 中处理线程时如何防止重复的标头?
我编写了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过会话变量吗?
您可以在每个脚本完成/成功时在每个脚本中设置一个会话变量,然后检查/等待它们在主进程中是否完成/成功。
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.