gpt4 book ai didi

php - 流式响应不起作用

转载 作者:行者123 更新时间:2023-12-04 18:13:17 24 4
gpt4 key购买 nike

我有一个漫长的导入过程,我正在尝试使用新的 StreamedResponse在 Symfony 2.1 中可以向用户报告有关任务的一些反馈,但响应没有被流式传输(我在处理结束时立即获得所有内容)。这是我在 Controller 中的代码:

            $em = $this->getDoctrine()->getEntityManager();
$response = new StreamedResponse();
$response->setCallback(function () use ($em) {
$file = fopen(sys_get_temp_dir().'/categories.txt', 'r');
$lineNum = 0;
while ($line = fgets ($file)) {
$category = new Category();
$fields = explode("\t",$line);
$category->setFullId($fields[0]);
$category->setName($fields[2]);
$category->setFullName($fields[4]);
$em->persist($category);
if ($lineNum%100==0) {
echo 'Processing Line: '.$lineNum.'<br>';
flush();
$em->flush();
}
$lineNum++;
}
fclose($file);
});
return $response;

知道可能出了什么问题吗?

最佳答案

好的,我找到了:您需要同时调用 ob_flush()flush() .

关于php - 流式响应不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12198919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com